假设我在LinearLayout中有几个按钮,其中2个是:
mycards_button = ((Button)this.findViewById(R.id.Button_MyCards));
exit_button = ((Button)this.findViewById(R.id.Button_Exit));
我在两者上注册setOnClickListener()
:
mycards_button.setOnClickListener(this);
exit_button.setOnClickListener(this);
如何使SWITCH区分Onclick中的两个按钮?
public void onClick(View v) {
switch(?????){
case ???:
/** Start a new Activity MyCards.java */
Intent intent = new Intent(this, MyCards.class);
this.startActivity(intent);
break;
case ???:
/** AlerDialog when click on Exit */
MyAlertDialog();
break;
}
答案 0 :(得分:115)
使用:
public void onClick(View v) {
switch(v.getId()){
case R.id.Button_MyCards: /** Start a new Activity MyCards.java */
Intent intent = new Intent(this, MyCards.class);
this.startActivity(intent);
break;
case R.id.Button_Exit: /** AlerDialog when click on Exit */
MyAlertDialog();
break;
}
}
请注意,这不适用于Android库项目(由于http://tools.android.com/tips/non-constant-fields),您需要使用以下内容:
int id = view.getId();
if (id == R.id.Button_MyCards) {
action1();
} else if (id == R.id.Button_Exit) {
action2();
}
答案 1 :(得分:7)
另一种选择是在setOnClickListener()中添加一个新的OnClickListener作为参数并覆盖onClick() - 方法:
mycards_button = ((Button)this.findViewById(R.id.Button_MyCards));
exit_button = ((Button)this.findViewById(R.id.Button_Exit));
// Add onClickListener to mycards_button
mycards_button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Start new activity
Intent intent = new Intent(this, MyCards.class);
this.startActivity(intent);
}
});
// Add onClickListener to exit_button
exit_button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Display alertDialog
MyAlertDialog();
}
});
答案 2 :(得分:5)
public class MainActivity extends Activity
implements View.OnClickListener {
private Button btnForward, btnBackword, btnPause, btnPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControl();
}
private void initControl() {
btnForward = (Button) findViewById(R.id.btnForward);
btnBackword = (Button) findViewById(R.id.btnBackword);
btnPause = (Button) findViewById(R.id.btnPause);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnForward.setOnClickListener(this);
btnBackword.setOnClickListener(this);
btnPause.setOnClickListener(this);
btnPlay.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnForward:
break;
case R.id.btnBackword:
break;
case R.id.btnPause:
break;
case R.id.btnPlay:
break;
}
}
}
答案 3 :(得分:2)
在OnCreate方法中: -
{
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener((View.OnClickListener)this);
b = (Button)findViewById(R.id.button2);
b.setOnClickListener((View.OnClickListener)this);
}
@Override
public void OnClick(View v){
switch(v.getId()){
case R.id.button1:
//whatever
break;
case R.id.button2:
//whatever
break;
}
答案 4 :(得分:1)
还有第三种选择。在onCreate()方法中,找到您拥有的所有按钮视图,并将它们保存为类数据成员。然后,您可以级联一组if-else语句以查找哪个。它有点混乱,但如果你不知道按钮的ID(如果你在java代码中生成按钮可能会很复杂),这是必须的。
@Override
public void onClick(View v) {
if (v == m_myCards) {
Intent intent = new Intent(this, MyCards.class);
this.startActivity(intent);
}
else if (v == m_exit) {
MyAlertDialog();
}
else if (v == m_back) {
finish();
}
这项技术的另一个好处是它灵活而快速(无需解析ID)。不好的是你需要将小部件保留在内存中。
不知道哪种方法更好。
答案 5 :(得分:0)
I make it simple, if the layout is same i just put the intent it.
My code like this:
public class RegistrationMenuActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnCertificate, btnSeminarKit;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration_menu);
initClick();
}
private void initClick() {
btnCertificate = (Button) findViewById(R.id.btn_Certificate);
btnCertificate.setOnClickListener(this);
btnSeminarKit = (Button) findViewById(R.id.btn_SeminarKit);
btnSeminarKit.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_Certificate:
break;
case R.id.btn_SeminarKit:
break;
}
Intent intent = new Intent(RegistrationMenuActivity.this, ScanQRCodeActivity.class);
startActivity(intent);
}
}
答案 6 :(得分:0)
对于我的示例:首先,“ MainActivity”实现了“ View.OnClickListener”,而不是开始执行代码...。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();}
public void init(){
foryou = (Button) this.findViewById(R.id.btn_foryou);
following = (Button) findViewById(R.id.btn_following);
popular = (Button) findViewById(R.id.btn_popular);
watching = (Button) findViewById(R.id.btn_continuewatching);
mProgress = (ProgressBar) findViewById(R.id.pb);
foryou.setOnClickListener(this);
following.setOnClickListener(this);
popular.setOnClickListener(this);
watching.setOnClickListener(this);
mProgress.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_foryou:
foryou.setPaintFlags(foryou.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
break;
case R.id.btn_following:
following.setPaintFlags(following.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
break;
case R.id.btn_popular:
popular.setPaintFlags(popular.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
break;
case R.id.btn_continuewatching:
watching.setPaintFlags(watching.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
break;
case R.id.btn_5:
// foryou.setPaintFlags(foryou.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
break;
default:
foryou.setPaintFlags(foryou.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
}
答案 7 :(得分:0)
还有另一种好方法,它不会浪费您的时间
创建方法来做任何你想做的事情,例如在你的活动中
public void replaceSuggest(View v) {
optionPanel.setVisibility(View.VISIBLE);
replaceWith.setVisibility(View.VISIBLE);
searchEdit.setText(selected);
}
创建方法后,转到 xml 视图布局并像这样放置(在要应用此操作的视图中,这里的示例是 TextView 将执行单击:
<TextView
android:id="@+id/anyId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/replace"
android:textColor="#ffffff"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="4dp"
android:textSize="12dp"
// here you write the methode name after onClick
android:onClick="replaceSuggest"/>