android 入门demo 事件监听
16lz
2021-12-20
package com.isoftstone.cry;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnFocusChangeListener;import android.view.View.OnKeyListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.Toast;public class EventListenerActivity extends Activity{private EditText myEdit1,myEdit2 ;private CheckBox cb1 ;private Button btn1,btn2 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.event_listener_layout);//获取组件对象myEdit1 = (EditText)this.findViewById(R.id.editText1);myEdit2 = (EditText)this.findViewById(R.id.editText2);cb1 = (CheckBox)this.findViewById(R.id.checkbox1);btn1 = (Button)this.findViewById(R.id.button01);btn2 = (Button)this.findViewById(R.id.button02);//响应事件myEdit1.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {// TODO Auto-generated method stubmyEdit1.setText("");return false;}});myEdit1.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {// TODO Auto-generated method stub//使用toast显示内容Toast.makeText(getApplicationContext(),myEdit1.getText(), Toast.LENGTH_LONG);}});myEdit2.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {// TODO Auto-generated method stubmyEdit2.setText("");return false;}});myEdit2.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {// TODO Auto-generated method stub//使用toast显示内容myEdit1.setText("获取焦点");//Toast.makeText(getApplicationContext(),myEdit2.getText(), Toast.LENGTH_SHORT);}});cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(),cb1.isChecked()+"",Toast.LENGTH_LONG);}});btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(),btn1.getText(),Toast.LENGTH_LONG);}});btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(),btn2.getText(),Toast.LENGTH_LONG);}});}}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableLayout android:id="@+id/tableLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableRow> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/username" /> <EditText android:id="@+id/editText1" android:text="admin" android:layout_width="fill_parent" android:scrollHorizontally="true"></EditText> </TableRow> <TableRow> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/password" /> <EditText android:id="@+id/editText2" android:password="true" android:text="123" android:layout_width="fill_parent" android:scrollHorizontally="true"></EditText> </TableRow> <TableRow> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/autoLogin" /> <CheckBox android:text="" android:id="@+id/checkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content"> </CheckBox> </TableRow> <TableRow> <Button android:text="login" android:id="@+id/button01"/> <Button android:text="canncel" android:id="@+id/button02"/> </TableRow> </TableLayout></LinearLayout>
更多相关文章
- Android(安卓)App: 使用Android(安卓)版Gmail App,更快地回复和搜
- android oncreate获取宽高度
- android从网络获取图片以及异步加载图片
- 【Android】上周问题记录
- android 性能优化
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程