view初步

控件与监听器

 为控件绑定监听器的流程

1) 获取代表控件的对象

2) 定义一个类,实现监听器接口

3) 生成监听器对象

4) 为控件绑定监听器对象

具体代码实现过程:

a. 在布局文件(acticity_main.xml里面添加上一个(button)控件及它的属性;

b. 在文件MainActivity.java中新建一个button对象(方法:private  Button button需导入<ctrl+shift+o>),它代表了布局文件中所声明的button

c. 通过Button=(Button<向下转型转成button类型>)findViewById(R.id.button);的方法,通过idbutton找出来;

d. 定义一个监听器(ButtonListener 内部类)并且要实现监听器接口(class ButtonListener implments OnClickListener{};备注:按照java的语法,应该去复写里边的抽象方法,抽象方法的名字叫做onClick,

<抽象方法的作用:当监听器(ButtonListener 类)的对象绑定在button上,当这个button按钮被点击时,就会执行这个对象onClick方法;>

e. 生成一个监听器对象(ButtonListener buttonListener = new ButtonListener()),

还的有button.setOnClickListener(buttonListener);将监听器对象设置给button

代码示例:

Activity_main.xml:

<button

  Android:id =”@+id/button”

  Android:layout_width=”match_parent ”

  Android:layout_height=”wrap_content”

  Android:text=”button”  >

MainActivity.java:

Private TextView textView;

Private Button button;

int count = 0;

//

SetContentView(R.layout.activity_main);

textView = (TextView)findViewById(R.id.textVeiw);

button =  (Button) findViewById(R.id.button);

ButtonListener buttonListener = new ButtonListener;

button.setOnClickListener(buttonListener);

//

Calss ButtonListener implements OnClickListener{

public void onClick(View arg0){

count++;

textView.setText(count+” ”);

 

}

    radioButton实例:package com.whk.radiobutton;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;


public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton femaleButton;
private RadioButton maleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

radioGroup=(RadioGroup)findViewById(R.id.radioGroupId);
femaleButton=(RadioButton)findViewById(R.id.femaleButtonId);
maleButton=(RadioButton)findViewById(R.id.malButtonId);

RadioGroupListener listener = new RadioGroupListener();
radioGroup.setOnCheckedChangeListener(listener);

}
  class RadioGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup arg0, int checkedId) {
if(checkedId == femaleButton.getId()){
System.out.println("femaleId");
}
else if(checkedId==maleButton.getId()){
System.out.println("maleId");
}
}
  }
@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;
}






}


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;


public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton femaleButton;
private RadioButton maleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

radioGroup=(RadioGroup)findViewById(R.id.radioGroupId);
femaleButton=(RadioButton)findViewById(R.id.femaleButtonId);
maleButton=(RadioButton)findViewById(R.id.malButtonId);

RadioGroupListener listener = new RadioGroupListener();
radioGroup.setOnCheckedChangeListener(listener);

}
  class RadioGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup arg0, int checkedId) {
if(checkedId == femaleButton.getId()){
System.out.println("femaleId");
}
else if(checkedId==maleButton.getId()){
System.out.println("maleId");
}
}
  }
@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;
}






}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值