各類監聽事件java
1.按鈕 Button
(1)點擊監聽
btn_1.setOnClickListener(new View.OnClickListener() {android
(2)長按監聽
btn_1.setOnLongClickListener(new View.OnLongClickListener() {設計模式
2.單選框 RadioGroup
radio_gp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {數組
3.複選框 CheckBox(普通內部類)
cb_fuxuan1.setOnCheckedChangeListener(new checkboxcheckedlistener());
cb_fuxuan2.setOnCheckedChangeListener(new checkboxcheckedlistener());多線程
private class checkboxcheckedlistener implements CompoundButton.OnCheckedChangeListener
{app
4.上下文菜單 ContextMenu(須要長按才能觸發)ide
changan_menu.setOnCreateContextMenuListener(this);
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//獲取值
public boolean onContextItemSelected(MenuItem item) {
item.getItemId()ui
5.進度條 SeekBar(可拖動)this
sbr_td.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {spa
6.開關 開關按鈕:ToggleButton 推拉開關 Switch
tb.setOnCheckedChangeListener(new anniucheckedlistener());
private class anniucheckedlistener implements CompoundButton.OnCheckedChangeListener{
7.對話框 方法鏈構造 new AlertDialog.Builder(this)
單選對話框
final String[] yanse = {"紅","黃","綠","藍","白"};
.setSingleChoiceItems(yanse, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
多選對話框
final String[] yanse = {"紅","黃","綠","藍","白"};
final boolean[] bl = {true,true,false,false,false};
.setMultiChoiceItems(yanse, bl, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
自定義對話框 加載器 getLayoutInflater() setview
LayoutInflater layoutInflater = getLayoutInflater();
8.進度條 ProgressDialog
旋轉進度條
final ProgressDialog pd = new ProgressDialog(this);
水平進度條
ProgressDialog pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
9.日期對話框
Calendar cl = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this,監聽, cl.get(Calendar.YEAR),cl.get(Calendar.MONTH),cl.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
監聽 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}
10.時間對話框
//獲取當前日期
//單例模式,設計模式的一種 靜態方法
Calendar cl = Calendar.getInstance();
TimePickerDialog timePickerDialog = new TimePickerDialog(this,監聽,cl.get(Calendar.HOUR),cl.get(Calendar.MINUTE),true);
timePickerDialog.show();
監聽 = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}
11.ListView
(1)ArrayAdapter
//構造適配器
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.listview_layout,list);
//設置適配器
listview_1.setAdapter(adapter);
//監聽事件
listview_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
(2)SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this,im,R.layout.simple_layout,str,viewid);
simple_1.setAdapter(simpleAdapter);
simple_1.setOnItemClickListener();
(3)BaseAdapter
12.自動提示文本框
AutoCompleteTextView autv_1 = (AutoCompleteTextView)findViewById(R.id.atv_1);
13下拉列表
Spinner sper_1 = (Spinner)findViewById(R.id.sper_1);
sper_1.setAdapter(adapter);
sper_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
14.消息提示
Resources res = Activitywenben.this.getResources();
//1.獲取狀態欄消息管理器
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//2.構建消息 用Builder構建 方法鏈調用
Notification nt = new Notification.Builder(this)
//3.交給管理器發出消息
manager.notify(0,nt);
xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview1" android:text="有連接嗎?" android:autoLink="all" /> <AutoCompleteTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/autv_1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕的監聽" android:id="@+id/btn_1" android:background="@drawable/anniu1" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/radio_gp"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是" android:id="@+id/rb_shi"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="否" android:id="@+id/rb_fou"/> </RadioGroup> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="hello world" android:id="@+id/et_1"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="複選一" android:id="@+id/cb_fuxuan1"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="複選二" android:id="@+id/cb_fuxuan2" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="長按觸發上下文菜單" android:id="@+id/menu_1"/> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:id="@+id/sbr_tuodong"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/anniu2" android:id="@+id/iv_bian" /> <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="開" android:textOff="關" android:id="@+id/tgb_1"/> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/swh_1"/> <!--對話框--> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發普通對話框" android:onClick="putongdhkonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發單選對話框" android:onClick="danxuandhkonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發多選對話框" android:onClick="duoxuandhkonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發自定義對話框" android:onClick="zidingyidhkonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發旋轉進度條" android:onClick="xuanzhuanjdtonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發水平進度條" android:onClick="shuipingjdtonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發日期對話框" android:onClick="riqidhkonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點擊觸發時間對話框" android:onClick="shijiandhkonclick"/> </LinearLayout> </ScrollView>
java
package com.example.chenshuai.test322; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.util.Linkify; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.SeekBar; import android.widget.Switch; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import android.widget.ToggleButton; import java.util.Calendar; /** * Created by chenshuai on 2016/4/1. */ public class Jianting extends AppCompatActivity { ImageView iv_bian; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.jiantinglayout); //Textview TextView textview1 = (TextView)findViewById(R.id.textview1); textview1.setAutoLinkMask(Linkify.ALL); String linktext = "百度連接 www.baidu.com"; textview1.setText(linktext); //AutoCompleteTextView AutoCompleteTextView autv_1 = (AutoCompleteTextView)findViewById(R.id.autv_1); String[] str = {"ab","abc","abcd"}; ArrayAdapter<String> stringArrayAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str); autv_1.setAdapter(stringArrayAdapter); //Button 點擊 Button btn_1 = (Button)findViewById(R.id.btn_1); btn_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(Jianting.this, "按鈕點擊監聽", Toast.LENGTH_SHORT).show(); } }); //Button 長按監聽 btn_1.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(Jianting.this, "按鈕長按監聽", Toast.LENGTH_SHORT).show(); return false; } }); //RadioGroup 的監聽 RadioGroup radio_gp = (RadioGroup)findViewById(R.id.radio_gp); radio_gp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb_shi = (RadioButton) findViewById(R.id.rb_shi); RadioButton rb_fou = (RadioButton) findViewById(R.id.rb_fou); switch (checkedId) { case R.id.rb_shi: Toast.makeText(Jianting.this, "選中了" + rb_shi.getText(), Toast.LENGTH_SHORT).show(); case R.id.rb_fou: Toast.makeText(Jianting.this, "選中了" + rb_fou.getText(), Toast.LENGTH_SHORT).show(); } } }); //顯示Edittext的輸入內容 EditText editText = (EditText)findViewById(R.id.et_1); String str1 = editText.getText().toString(); Toast.makeText(Jianting.this, str1, Toast.LENGTH_SHORT).show(); //checkbox的監聽 CheckBox cb_fuxuan1 = (CheckBox)findViewById(R.id.cb_fuxuan1); cb_fuxuan1.setOnCheckedChangeListener(new checkboxcheckedlistener()); CheckBox cb_fuxuan2 = (CheckBox)findViewById(R.id.cb_fuxuan2); cb_fuxuan2.setOnCheckedChangeListener(new checkboxcheckedlistener()); //menu 菜單 上下文菜單 Button changan_menu = (Button)findViewById(R.id.menu_1); changan_menu.setOnCreateContextMenuListener(this); //SeekBar 可拖動進度條 SeekBar sbr_td = (SeekBar)findViewById(R.id.sbr_tuodong); sbr_td.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); //開關鍵控制按鈕背景 iv_bian = (ImageView)findViewById(R.id.iv_bian); //開關按鈕 ToggleButton tb = (ToggleButton)findViewById(R.id.tgb_1); tb.setOnCheckedChangeListener(new anniucheckedlistener()); //推拉開關 Switch swh = (Switch)findViewById(R.id.swh_1); swh.setOnCheckedChangeListener(new anniucheckedlistener()); } //普通內部類 checkbox的監聽 private class checkboxcheckedlistener implements CompoundButton.OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { CheckBox cb = (CheckBox)buttonView; if (isChecked) { Toast.makeText(Jianting.this, "選中了"+cb.getText(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Jianting.this, "取消選中了"+cb.getText(), Toast.LENGTH_SHORT).show(); } } } //menu 菜單 上下文菜單 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(1,1,1,"添加"); menu.add(1,2,2,"刪除"); menu.add(1,3,3,"修改"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(Jianting.this, "觸發了添加功能", Toast.LENGTH_SHORT).show(); case 2: Toast.makeText(Jianting.this, "觸發了刪除功能", Toast.LENGTH_SHORT).show(); case 3: Toast.makeText(Jianting.this, "觸發了修改功能", Toast.LENGTH_SHORT).show(); } return super.onContextItemSelected(item); } private class anniucheckedlistener implements CompoundButton.OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { iv_bian.setImageResource(R.drawable.anniu1); } else { iv_bian.setImageResource(R.drawable.anniu2); } } } //普通對話框 public void putongdhkonclick(View view) { //普通對話框 //對話框的構建器 /* AlertDialog.Builder ab = new AlertDialog.Builder(this); ab.setTitle("數據刪除"); ab.setMessage("肯定刪除嗎?"); ab.setPositiveButton("肯定",null); ab.setNegativeButton("取消",null); ab.setCancelable(false); ab.show();*/ //方法鏈的方法 new AlertDialog.Builder(this) .setTitle("數據刪除") .setMessage("肯定刪除嗎?") .setPositiveButton("肯定",null) .setNegativeButton("取消",null) .setCancelable(false) .show(); } public void danxuandhkonclick(View view) { final String[] yanse = {"紅","黃","綠","藍","白"}; //方法鏈 new AlertDialog.Builder(this) .setTitle("單選對話框") .setSingleChoiceItems(yanse, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Jianting.this, "選中了" + yanse[which], Toast.LENGTH_SHORT).show(); //移除屬性 dialog.dismiss(); 選中一個就會關閉 } }) .setNeutralButton("肯定", null)//普通按鈕 .setCancelable(false) .show(); } public void duoxuandhkonclick(View view) { final String[] yanse = {"紅","黃","綠","藍","白"}; final boolean[] bl = {true,true,false,false,false}; //方法鏈 new AlertDialog.Builder(this) .setTitle("多選對話框") .setMultiChoiceItems(yanse, bl, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(Jianting.this, "選中了" + yanse[which], Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Jianting.this, "取消選中了" + yanse[which], Toast.LENGTH_SHORT).show(); } } }) .setNeutralButton("肯定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //遍歷數組 foreach循環 for (boolean b : bl) { try { Thread.sleep(100); } catch (Exception ex) { }Toast.makeText(Jianting.this, "取值"+b, Toast.LENGTH_SHORT).show(); } } }) .setCancelable(false) .show(); } //自定義對話框 public void zidingyidhkonclick(View view) { //1.獲取加載器 LayoutInflater layoutInflater = getLayoutInflater(); //2.用加載器加載文件 final View view2 = layoutInflater.inflate(R.layout.loginlayout, null); //方法鏈構造頁面加兩個按鈕 new AlertDialog.Builder(this) .setView(view2)//兼容性好,比較適用 //.setView(R.layout.loginlayout) .setNegativeButton("取消", null) .setPositiveButton("登錄", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText user = (EditText) view2.findViewById(R.id.et_username); EditText pas = (EditText) view2.findViewById(R.id.et_password); } }) .show(); } //旋轉進度條 public void xuanzhuanjdtonclick(View view) { final ProgressDialog pd = new ProgressDialog(this); pd.setMessage("正在加載"); pd.show(); //建立thread實例 =【重寫run方法 啓動多線程 new Thread() { @Override public void run() { super.run(); try{ Thread.sleep(3000); } catch (Exception ex){} pd.dismiss();//關閉 } }.start(); } //水平進度條 public void shuipingjdtonclick(View view) { ProgressDialog pd = new ProgressDialog(this); pd.setMessage("正在加載"); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.show(); } //日期對話框 public void riqidhkonclick(View view) { //獲取當前日期 //單例模式,設計模式的一種 靜態方法 Calendar cl = Calendar.getInstance(); DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(Jianting.this, year+"-"+ (monthOfYear+1) + "-" + dayOfMonth, Toast.LENGTH_SHORT).show(); } },cl.get(Calendar.YEAR),cl.get(Calendar.MONTH),cl.get(Calendar.DAY_OF_MONTH)); datePickerDialog.setCancelable(false); datePickerDialog.show(); } //時間對話框 public void shijiandhkonclick(View view) { //獲取當前日期 //單例模式,設計模式的一種 靜態方法 Calendar cl = Calendar.getInstance(); TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(Jianting.this, hourOfDay+":"+minute , Toast.LENGTH_SHORT).show(); } },cl.get(Calendar.HOUR),cl.get(Calendar.MINUTE),true); timePickerDialog.setCancelable(false); timePickerDialog.show(); } }