android onclick方法吗,Android中button的onClick事件几种方法
Android中button的onClick事件幾種方法
發(fā)布時(shí)間:2020-09-03 02:36:31
來(lái)源:腳本之家
閱讀:100
作者:水桶前輩
Android中button的onClick事件幾種方法
利用三種方法,學(xué)習(xí)button的監(jiān)聽(tīng)事件。
方法一源碼如下:
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
//import android.text.Editable;
//import android.text.TextWatcher;
import android.widget.EditText;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
//方法一:
//增加事件響應(yīng)
button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent =new Intent();
intent.setClass(MainActivity.this, TestActivity.class);
MainActivity.this.startActivity(intent);
}
});
button2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent =new Intent();
intent.setClass(MainActivity.this, TActivity.class);
MainActivity.this.startActivity(intent);
}
});
}
方法二:
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
//import android.text.Editable;
//import android.text.TextWatcher;
import android.widget.EditText;
public class MainActivity extends Activity
{
@Override
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
}
//方法二:
public void myClickHandler(View target){
switch (target.getId()) {
case R.id.button1:
Intent music=new Intent();
music.setClass(MainActivity.this, TestActivity.class);
MainActivity.this.startActivity(music);
// setTitle("myClickHandler01");
break;
case R.id.button2:
// setTitle("myClickHandler02");
Intent moive=new Intent();
moive.setClass(MainActivity.this, TActivity.class);
MainActivity.this.startActivity(moive);
break;
}
}
}
方法三:
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
//import android.text.Editable;
//import android.text.TextWatcher;
import android.widget.EditText;
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//方法三:
public void myClickHandler01(View target){
//設(shè)置activity的頭標(biāo)題
// setTitle("myClickHandler01");
Intent music=new Intent();
music.setClass(MainActivity.this, TestActivity.class);
MainActivity.this.startActivity(music);
}
public void myClickHandler02(View target){
//設(shè)置activity的頭標(biāo)題
// setTitle("myClickHandler02");
Intent moive=new Intent();
moive.setClass(MainActivity.this, TActivity.class);
MainActivity.this.startActivity(moive);
}
}
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
總結(jié)
以上是生活随笔為你收集整理的android onclick方法吗,Android中button的onClick事件几种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 流内容html,为什么,和元素属于htm
- 下一篇: java连接并操作redis_java