在android中button响应的两种方式
2013-08-15 11:34
232 查看
在android中button响应的几种方式
1.
在布局文件中添加button的监听名字
Android:onClick=”buttonOnClick”
例如:
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/textView2"
android:layout_marginTop="28dp"
android:text="Button"
android:onClick="buttonOnClick"
/>
在activity中创建响应的函数
publicvoid buttonOnClick(View view){
Mytext1.setText("hello!");//设置文档的显示
}
2.
在OnCreate函数中写
Mybutton = (Button)findViewById(R.id.button1);
Mybutton.setOnClickListener(new OnClickListener(){
@Override
publicvoid onClick(View v) {
//
TODO Auto-generated method stub
Mytext.setText("hello!");//设置文档的显示
}
});
3.在OnCreate函数中写
findViewById(R.id.btn_title_popmenu).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
再来
publicvoid onClick(View v) {
if(v.getId() == R.id.btn_title_popmenu){
popMenu.showAsDropDown(v);
}elseif(v.getId()==R.id.button1)
{
popMenu1.showAsDropDown(v);
}
}
前提条件是:
public
class MainActivity extends Activity implements OnClickListener{}
第三种方法例子:
publicclass MainActivity
extends Activity
implements OnClickListener{
private TextView
Mytext =
null;
private TextView
Mytext1 =
null;
@Override
//第一次运行activity时会运行
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//设置要显示的控件
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
Mytext = (TextView)findViewById(R.id.textView1);
Mytext1 = (TextView)findViewById(R.id.textView2);
}
@Override
publicvoid onClick(View arg0) {
//
TODO自动生成的方法存根
if(arg0.getId()==R.id.button1){
Mytext.setText("hello!");//设置文档的显示
}
elseif(arg0.getId()==R.id.button2){
Mytext1.setText("hello!");//设置文档的显示
}
}
前两种方法的例子:
public class MainActivity extends Activity {
private Button Mybutton = null;
private Button Mybutton1 = null;
private TextView Mytext = null;
private TextView Mytext1 = null;
@Override
//第一次运行activity时会运行
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//设置要显示的控件
Mybutton = (Button)findViewById(R.id.button1);
Mytext = (TextView)findViewById(R.id.textView1);
Mytext1 = (TextView)findViewById(R.id.textView2);
//第二种方法
Mybutton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
Mytext.setText("hello!");//设置文档的显示
}
});
}
//第一种方法
public void button2OnClick(View view){
Mytext1.setText("hello!");//设置文档的显示
}
//第一种方法要在button的布局文件中设置android:onClick="buttonOnClick"
1.
在布局文件中添加button的监听名字
Android:onClick=”buttonOnClick”
例如:
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/textView2"
android:layout_marginTop="28dp"
android:text="Button"
android:onClick="buttonOnClick"
/>
在activity中创建响应的函数
publicvoid buttonOnClick(View view){
Mytext1.setText("hello!");//设置文档的显示
}
2.
在OnCreate函数中写
Mybutton = (Button)findViewById(R.id.button1);
Mybutton.setOnClickListener(new OnClickListener(){
@Override
publicvoid onClick(View v) {
//
TODO Auto-generated method stub
Mytext.setText("hello!");//设置文档的显示
}
});
3.在OnCreate函数中写
findViewById(R.id.btn_title_popmenu).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
再来
publicvoid onClick(View v) {
if(v.getId() == R.id.btn_title_popmenu){
popMenu.showAsDropDown(v);
}elseif(v.getId()==R.id.button1)
{
popMenu1.showAsDropDown(v);
}
}
前提条件是:
public
class MainActivity extends Activity implements OnClickListener{}
第三种方法例子:
publicclass MainActivity
extends Activity
implements OnClickListener{
private TextView
Mytext =
null;
private TextView
Mytext1 =
null;
@Override
//第一次运行activity时会运行
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//设置要显示的控件
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
Mytext = (TextView)findViewById(R.id.textView1);
Mytext1 = (TextView)findViewById(R.id.textView2);
}
@Override
publicvoid onClick(View arg0) {
//
TODO自动生成的方法存根
if(arg0.getId()==R.id.button1){
Mytext.setText("hello!");//设置文档的显示
}
elseif(arg0.getId()==R.id.button2){
Mytext1.setText("hello!");//设置文档的显示
}
}
前两种方法的例子:
public class MainActivity extends Activity {
private Button Mybutton = null;
private Button Mybutton1 = null;
private TextView Mytext = null;
private TextView Mytext1 = null;
@Override
//第一次运行activity时会运行
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//设置要显示的控件
Mybutton = (Button)findViewById(R.id.button1);
Mytext = (TextView)findViewById(R.id.textView1);
Mytext1 = (TextView)findViewById(R.id.textView2);
//第二种方法
Mybutton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
Mytext.setText("hello!");//设置文档的显示
}
});
}
//第一种方法
public void button2OnClick(View view){
Mytext1.setText("hello!");//设置文档的显示
}
//第一种方法要在button的布局文件中设置android:onClick="buttonOnClick"
相关文章推荐
- 转:在android中button响应的两种方式
- Android------Button 添加声音效果(两种方式)
- Android - ScrollView滑动到底部或者顶部的两种响应方式
- Android自定义button点击效果的两种方式
- Android------Button 添加声音效果(两种方式)
- android 用户界面笔记之Button和ImageButton实现按钮响应事件两种方法
- Android------Button 添加声音效果(两种方式)
- 在android中button响应的几种方式
- 【Android】Android开发之常用的loading等待效果实现,仿微博等待动画。两种实现方式详解
- android 两种设置全屏的activity方式
- 横竖屏事件响应(viewWillLayoutSubviews和通知)两种方式
- Android获取天气预报的两种方式(国家气象局和中央气象台天气预报API)
- Android中两种序列化方式的比较Serializable和Parcelable
- 【重头学习Android】关于广播的两种方式
- Android中,关于service的启动两种方式描述不正确的是
- android两种Tab分页的方式:TabActivity和ActivityGroup
- 设置android全屏的两种方式
- 总结的Android中surfaceView清屏的方式。两种。(目的是为了自己方便回顾)仅供参考。
- android学习(1) - Button单击事件的响应