各种监听事件
1.按钮 Button
(1)点击监听
btn_1.setOnClickListener(new View.OnClickListener() {
(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
{
4.上下文菜单 ContextMenu(需要长按才能触发)
changan_menu.setOnCreateContextMenuListener(this);
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//获取值
public boolean onContextItemSelected(MenuItem item) {
item.getItemId()
5.进度条 SeekBar(可拖动)
sbr_td.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
6.开关 开关按钮:ToggleButton 推拉开关 Switch
tb.setOnCheckedChangeListener(new anniucheckedlistener());
private class anniucheckedlistener implements CompoundButton.OnCheckedChangeListener{
xml
<?xml version="1.0" encoding="utf-8"?> <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"/> </LinearLayout>
java
package com.example.chenshuai.test322; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.util.Linkify; import android.view.ContextMenu; 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.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.Toast; import android.widget.ToggleButton; /** * 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); } } } }