Android:对超过 1 个按钮使用带有 setOnClickListener/onClick 的 SWITCH 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1504160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Use a SWITCH statement with setOnClickListener/onClick for more than 1 button?
提问by Hubert
Let's say I have a few buttons in a LinearLayout, 2 of them are:
假设我在 LinearLayout 中有几个按钮,其中 2 个是:
mycards_button = ((Button)this.findViewById(R.id.Button_MyCards));
exit_button = ((Button)this.findViewById(R.id.Button_Exit));
I register setOnClickListener()
on both of them:
我setOnClickListener()
在他们两个上注册:
mycards_button.setOnClickListener(this);
exit_button.setOnClickListener(this);
How do I make a SWITCH to differentiate between the two buttons within the Onclick ?
如何制作 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;
}
回答by Intrications
Use:
用:
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;
}
}
Note that this will not work in Android library projects (due to http://tools.android.com/tips/non-constant-fields) where you will need to use something like:
请注意,这不适用于 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();
}
回答by aspartame
Another option is to add a new OnClickListener as parameter in setOnClickListener() and overriding the onClick()-method:
另一种选择是在 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();
}
});
回答by DroidVilla
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;
}
}
}
回答by ayushnvijay
inside OnCreate method :-
在 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;
}
回答by Scott Biggs
And there's yet a third option. In your onCreate() method, find all the button views that you have and save them as class data members. You can then cascade a group of if-else statements to find which is which. It's kind of messy, but it's a must if you don't know the ID of the buttons (which can be complicated if you are generating buttons in java code).
还有第三种选择。在您的 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();
}
Another nice thing about this technique is that it's flexible and quick (no having to parse IDs). The bad thing is that you need to keep the widgets in memory.
这种技术的另一个好处是它灵活且快速(无需解析 ID)。坏处是您需要将小部件保留在内存中。
Don't know which method is better.
不知道哪种方法更好。
回答by Uray Febri
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);
}
}
回答by Chirag Patel
For my example :first 'MainActivity' implements 'View.OnClickListener' than start the code ....
对于我的例子:首先'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);
}
}