事件监听篇---第二篇

下面是各种常用控件的事件监听的使用

①EditText(编辑框)的事件监听---OnKeyListener

②RadioGroup、RadioButton(单选按钮)的事件监听---OnCheckedChangeListener

③CheckBox(多选按钮)的事件监听---OnCheckedChangeListener

④Spinner(下拉列表)的事件监听---OnItemSelectedListener

⑤Menu(菜单)的事件处理---onMenuItemSelected

⑥Dialog(对话框)的事件监听---DialogInterface.OnClickListener()



第一个例子:EditText的事件监听


Java代码
package org.hualang.eventtest2;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class EventTest2 extends Activity {
/** Called when the activity is first created. */
private TextView mytext;
private EditText edittext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mytext = (TextView)findViewById(R.id.mytext);
edittext = (EditText)findViewById(R.id.edittext);
/**
* 设置当EditText为空,则提示“请输入账号”
* 在配置文件main.xml中可以用android:hint="请输入账号"来实现
*/
edittext.setHint("请输入账号");
//下面为EditText事件监听
edittext.setOnKeyListener(new EditText.OnKeyListener()
{

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
//得到文字,显示在TextView中
mytext.setText("内容:"+edittext.getText().toString());
return false;
}

});
}
}

main.xml
Java代码

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mytext"
/>
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10pt"
/>




运行结果如下:









第二个例子:单选按钮的事件监听处理
Java代码
package org.hualang.eventtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class EventTest3 extends Activity {
/** Called when the activity is first created. */
private RadioGroup group;
private RadioButton radio1,radio2,radio3,radio4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

group = (RadioGroup)findViewById(R.id.radiogroup1);
radio1 = (RadioButton)findViewById(R.id.button1);
radio2 = (RadioButton)findViewById(R.id.button2);
radio3 = (RadioButton)findViewById(R.id.button3);
radio4 = (RadioButton)findViewById(R.id.button4);

group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == radio2.getId())
{
showMessage("正确答案:" + radio2.getText()+",恭喜你,答对了");
}
else
{
showMessage("对不起,虽然很多,但不是公认的最多");
}
}
});
}
public void showMessage(String str)
{
Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 220);
toast.show();
}
}

main.xml
Java代码

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/mytextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="哪个城市的美女最多?"
/>
android:id="@+id/radiogroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="杭州"
/>
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重庆"
/>
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成都"
/>
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="香港"
/>





运行结果如下:





第三个例子:复选框的事件处理
Java代码
package org.hualang.eventtest4;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class EventTest4 extends Activity {
/** Called when the activity is first created. */
private CheckBox ch1,ch2,ch3,ch4,ch5;
private Button mybutton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mybutton = (Button)findViewById(R.id.mybutton);
ch1 = (CheckBox)findViewById(R.id.check1);
ch2 = (CheckBox)findViewById(R.id.check2);
ch3 = (CheckBox)findViewById(R.id.check3);
ch4 = (CheckBox)findViewById(R.id.check4);
ch5 = (CheckBox)findViewById(R.id.check5);

ch1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(ch1.isChecked())
{
showMessage("你选择了"+ch1.getText());
}
}

});
ch2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(ch3.isChecked())
{
showMessage("你选择了"+ch2.getText());
}
}

});
ch3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(ch3.isChecked())
{
showMessage("你选择了"+ch3.getText());
}
}

});
ch4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(ch4.isChecked())
{
showMessage("你选择了"+ch4.getText());
}
}

});
ch5.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(ch5.isChecked())
{
showMessage("你选择了"+ch5.getText());
}
}

});

mybutton.setOnClickListener(new Button.OnClickListener()
{

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int num = 0;
if(ch1.isChecked())
{
num++;
}
if(ch2.isChecked())
{
num++;
}
if(ch3.isChecked())
{
num++;
}
if(ch4.isChecked())
{
num++;
}
if(ch5.isChecked())
{
num++;
}

showMessage("谢谢参与,您一共选择了"+num+"项");

}

});
}



public void showMessage(String str)
{
Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 220);
toast.show();
}
}


Java代码

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="你喜欢哪些智能手机系统"
/>
android:id="@+id/check1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="苹果 ios"
/>
android:id="@+id/check2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="谷歌 Android"
/>
android:id="@+id/check3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RIM BlackBerry"
/>
android:id="@+id/check4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="微软 Windows phone 7"
/>
android:id="@+id/check5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="诺基亚 symbian"
/>

你可能感兴趣的:(android,android,layout,radiobutton,import,dialog,encoding)