EditText以及checkBox简单例子

package com.example.edittext;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener,
		OnCheckedChangeListener {
	private Button btn;
	private TextView tv;
	private EditText ev;
	private CheckBox cb1, cb2, cb3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.bt);
		tv = (TextView) findViewById(R.id.tv);
		ev = (EditText) findViewById(R.id.et);
		btn.setOnClickListener(this);
		cb1 = (CheckBox) findViewById(R.id.cb1);
		cb2 = (CheckBox) findViewById(R.id.cb2);
		cb3 = (CheckBox) findViewById(R.id.cb3);
		cb1.setOnCheckedChangeListener(this);
		cb2.setOnCheckedChangeListener(this);
		cb3.setOnCheckedChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btn) {
			tv.setText(ev.getText().toString());
		}
	}
/**
 * 对CheckBox进行监听,步骤如下:
步骤1:使用OnCheckChangeListener接口,这里的接口导入的是:
	“android.widget.CompoundButton.OnCheckChangeListener”;
步骤2:重写监听器的抽象函数“onCheckedChanged()”
步骤3:将每个CheckBox组件绑定监听器。
  	通过重写的onCheckedChanged(CompoundButton buttonView,boolean isChecked)函数
  	一个参数来确定哪个CheckBox状态发生改变;根据第二个参数来确定改变的CheckeBox的具体状态值,true为勾选,false为未勾选。
 * makeText(Context context,CharSequence text,int duration)
  第一参数是上下文对象;第二个参数显示的文本内容;第三个参数显示提示消息的持续时间;其值有两个常量:LENGTH_SHORT(短暂持续)和LENGTH_LONG(略长持续)
 * */
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		//buttonView 选中状态发生改变的那个按钮  
        //isChecked 表示按钮新的状态(true/false)  
		if(cb1==buttonView || cb2==buttonView || cb3==buttonView){
			if(isChecked){
				toastDisplay(buttonView.getText()+"选中");
			}else{
				toastDisplay(buttonView.getText()+"取消选中");
			}
		}
	}
	public void toastDisplay(String str){
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();  
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="提示信息" />

    <Button
        android:id="@+id/bt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="获取EditText的内容" />
	<CheckBox  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/cb1"  
        android:id="@+id/cb1"  
        />  
    <CheckBox   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/cb2"  
        android:id="@+id/cb2"  
        />  
    <CheckBox  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/cb3"  
        android:id="@+id/cb3"  
        />  
</LinearLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值