我正在学习Android,我在主要活动中创建了两个按钮,然后从每个按钮我要打开新活动,按钮2正在工作但按钮1不起作用,我的代码:
public class MainActivity extends Activity {
Button btn;
Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button2);
btn2 = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Allactivities.class);
startActivity(intent);
Intent i = new Intent(MainActivity.this, Review.class);
startActivity(i);
}
});
}
}
Xml代码:
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_above="@+id/button1"
android:layout_marginBottom="26dp"
android:background="@drawable/btnbluee"
android:text="@string/button_tittle"
android:onClick="onClick" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="192dp"
android:background="@drawable/btnorange"
android:text="@string/button_title"
android:onClick="onClick" />
答案 0 :(得分:1)
按钮2正在工作但按钮1无效,
这是因为你需要为每个按钮添加一个监听器:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Allactivities.class);
startActivity(intent);
Intent i = new Intent(MainActivity.this,Review.class);
startActivity(i);
}
});
和
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Allactivities.class);
startActivity(intent);
Intent i = new Intent(MainActivity.this,Review.class);
startActivity(i);
}
});
只是一个建议..
尝试在布局和代码之间保持一致,你将不遗余力地在脖子上......
btn = (Button)findViewById(R.id.button2);
btn2= (Button)findViewById(R.id.button1);
必须是:
btn = (Button)findViewById(R.id.button1);
btn2= (Button)findViewById(R.id.button2);
答案 1 :(得分:0)
就这样做:
public class MainActivity extends Activity {
Button btn;
Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button2);
btn2 = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Review.class);
startActivity(i);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Allactivities.class);
startActivity(intent);
}
});
}
}
答案 2 :(得分:0)
试试这个:
//set listener button 2 (btn)
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Allactivities.class) ;
startActivity(intent) ;
}
});
//set listener button 1 (btn2)
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Review.class) ;
startActivity(i) ;
}
});
答案 3 :(得分:0)
所选答案还可以,但您可以使用此示例改进解决方案:
public class Main2Activity extends Activity implements View.OnClickListener {
Button btn;
Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()){
case R.id.button1: {
intent = new Intent(MainActivity.this, Allactivities.class);
}break;
case R.id.button2: {
intent = new Intent(MainActivity.this, Review.class);
}break;
}
if(intent != null){
startActivity(intent);
}
}
}
使用onClick
你可以处理要求他们的id的按钮动作,你会写更少的代码。我只发布我的解决方案以改进或提供另一种选择。