Android:SetonClickListener/OnClickを使用してSwitchステートメントを使用して1ボタンを超えますか?
質問
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);
オンクリック内の2つのボタンを区別するために切り替えを行うにはどうすればよいですか?
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;
}
解決
使用する:
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();
}
他のヒント
別のオプションは、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();
}
});
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;
}
}
}
内部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;
}
そして、まだ3番目のオプションがあります。 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();
}
この手法のもう1つの良い点は、柔軟で迅速であることです(IDを解析する必要はありません)。悪いことは、ウィジェットをメモリに保つ必要があるということです。
どの方法が優れているかわからない。
私はそれをシンプルにします、レイアウトが同じ場合、私はそれを意図しただけです。
このような私のコード:
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);
}
}
私の例:最初の「MainActivity」は、「view.onclickListener」をコードの開始よりも実装します。
@オーバーライド
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);
}
}