EditText、Button、Integer以及拨号

EditText可以设置一个hint属性,显示的是一个不可更改的文字,可以作为输入框的提示用,有数据输入时自动隐藏,比较好用


<EditText
        android:id="@+id/et_num"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入数字"
        android:textColorHint="#238745"/>
    <Button 
        android:id="@+id/bt_num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:text="加"/>
    <Button 
        android:id="@+id/bt_num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="减"/>
    <Button 
        android:id="@+id/bt_num3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拨号"/>

当有多个button在一个activity里时,用activity去实现onclicklisten,再去set各自的listener时,传一个this就好,这样无需多次implements

那么在需要用到不同的button时

通过获取各自的ID值去判断就好


对editText数据的获取,里面的是string,要变成int型

首选获取 getText().toString()

然后改变类型valueOf()

再设置内容setText()


public class MainActivity extends Activity implements OnClickListener{
    EditText et_num;
    Button bt1;
    Button bt2;
    Button bt3;
    int num;   
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et_num = (EditText) findViewById(R.id.et_num);
		//必须给一个初始值,否则在点击button时会空指针异常
		et_num.setText("1");
		num = Integer.valueOf(et_num.getText().toString());	
		
		bt1 = (Button) findViewById(R.id.bt_num1);
		bt2 = (Button) findViewById(R.id.bt_num2);
		bt3 = (Button) findViewById(R.id.bt_num3);
		
		bt1.setOnClickListener(this);
		bt2.setOnClickListener(this);
		bt3.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int button = v.getId();
		
			if (button == R.id.bt_num1) {
				System.out.println("bt1 called");
	            num++;  
	            settext(); 
			}else if(button == R.id.bt_num2){
				System.out.println("bt2 called");
				num--;
				settext(); 
			}else if (button == R.id.bt_num3) {
				System.out.println("bt3 called");
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+num));
				startActivity(intent);
			}
	}
	//给一个点击的范围
	private void settext() {
		// TODO Auto-generated method stub
		if (num <= 0 || num >= 10) {
			Toast.makeText(getApplication(), "超出范围喽", 0).show();
			/*
			 * 到了上下限时,比如,显示的是1
			 * 仍然多次点击,这样显示的数值不变,但是实际上的num值已变
			 * 反向点击button时,需要点击相应的次数才能再次从上下限开始变动
			 * 这些点击就好像是无用功,用户体验不好,所以需要获取到当前显示的上下限重新赋值
			 */
			num = Integer.valueOf(et_num.getText().toString());	
		} else {
			et_num.setText(Integer.toString(num));
		}
	}	
}

关于拨号,需要一个权限
Intent intent = new Intent();
				intent.setAction(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+num));
				startActivity(intent);

顺便提一下,关于int和integer

integer是一个包装类,int是基本数据类型

这个包装类就是为了方便我们做一些数据类型的转变

如上面获取号码,从string转到int,通过这个integer过渡了下


另外在泛型中,比如要给list填一个int类型数据,尖括号类必须是 类 ,所以也需要integer

List<Integer> nums;

Integer  a = new  Integer  (128);

Integer  b = new  Integer  (128);

a==b?

a.equals(b)?


Integer c = 10;

Integer d = 10;

c==d?

c.equals(d)?


false  true true  true


==判断的是引用,地址,new出不同对象就不等,所以第一个是false;但是在自动装箱中,如果装的是字节,很小,值相等时没有开辟新空间,所以相等

equals判断的是值,所以2、4都是true

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章