Android 常用控制項介面監聽

來源:互聯網
上載者:User

Android 常用控制項介面監聽

 

Android控制項監聽方面,用介面實現監聽是最好的,在Android 本身就提供了各種控制項監聽介面,我們只要按照這樣實現,看起來代碼會很整潔。實現的效果也是很好的,下面我列舉了常用控制項的介面監聽,layout ,checkbox,RadioGroup,以及listview的單擊或者長按監聽。下面請看代碼,有注釋。
package com.example.impletedemo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.ListView;import android.widget.RadioGroup;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements         OnClickListener,CompoundButton.OnCheckedChangeListener,RadioGroup.OnCheckedChangeListener,OnItemLongClickListener,OnItemClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InitUI();}private void InitUI() {View Layout1 = findViewById(R.id.Layout1);CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);//介面實現監聽,都是需要進行設定監聽if (Layout1 != null)Layout1.setOnClickListener(this);if (checkBox1 != null)checkBox1.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener)this);if (checkBox2 != null)checkBox2.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener)this);if (radioGroup1 != null)radioGroup1.setOnCheckedChangeListener((RadioGroup.OnCheckedChangeListener)this);ListView myListView = (ListView) findViewById(R.id.listView1);myListView.setOnItemLongClickListener(this); // 設定ListView長按myListView.setOnItemClickListener(this);    // 設定ListView單擊SimpleAdapter simpleAdapter = new SimpleAdapter(this, getDataSource(), R.layout.layout_list_item, new String[]{title}, new int[]{R.id.textView1});myListView.setAdapter(simpleAdapter);}public List> getDataSource() {List> listems = new ArrayList>();  Map map = new HashMap();  map.put(title, 北京);listems.add(map);map = new HashMap();map.put(title, 上海);listems.add(map);map = new HashMap();map.put(title, 廣州);listems.add(map);map = new HashMap();map.put(title, 南京);listems.add(map);map = new HashMap();map.put(title, 蘇州);listems.add(map);return listems;}@Override//一般layout button監聽實現方法public void onClick(View arg0) {if (arg0.getId() == R.id.Layout1) {TextView textView = (TextView) findViewById(R.id.textView1);textView.setText(layout監聽);Toast.makeText(this, layout監聽, Toast.LENGTH_SHORT).show();}}@Override//checkbox監聽實現方法public void onCheckedChanged(CompoundButton arg0, boolean arg1) {if (arg0.getId() == R.id.checkBox1) {if (arg1) {Toast.makeText(this, 選中單項1, Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, 取消單項1, Toast.LENGTH_SHORT).show();}} else if (arg0.getId() == R.id.checkBox2) {if (arg1) {Toast.makeText(this, 選中單項2, Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, 取消單項2, Toast.LENGTH_SHORT).show();}}}@Override//RadioGroup監聽實現方法public void onCheckedChanged(RadioGroup arg0, int arg1) {if (arg0.getId() == R.id.radioGroup1) {if (arg1 == R.id.radio1) {           Toast.makeText(this, 選中男, Toast.LENGTH_SHORT).show();}else if (arg1 == R.id.radio2) {   Toast.makeText(this, 選中女, Toast.LENGTH_SHORT).show();}}}@Override//listview長按監聽實現方法public boolean onItemLongClick(AdapterView arg0, View arg1, int arg2,long arg3) {Toast.makeText(this, listview 長按 + getDataSource().get(arg2), Toast.LENGTH_SHORT).show();//這裡返回一定是true , 不然會同時觸發單擊return true;   }@Override//listview單擊監聽實現方法public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {Toast.makeText(this, listview 單擊 + getDataSource().get(arg2), Toast.LENGTH_SHORT).show();}}

xml如下
                                                                                                

介面

下面看一下常用監聽介面,作為程式員,都會使用代碼自動提示以及填充。接下來看看:可以找到你需要的介面。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.