我已经制作了一个带有一堆按钮的应用程序,但只有第一个和第二个有效。我该怎么办?
这是我的代码:
package com.metoo.codedetective;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ceo = (Button) findViewById(R.id.codebutton);
ceo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Etelaateomoomi.class));
Button fr = (Button) findViewById(R.id.button2);
fr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Factoryreset.class));
Button pe = (Button) findViewById(R.id.button5);
pe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Endcallpower.class));
//button camera information
Button ci= (Button) findViewById(R.id.button4);
ci.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Camerainformation.class));
//backup button
Button bt= (Button) findViewById(R.id.button6);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, backuptaking.class));
//testing button
Button tb = (Button) findViewById(R.id.button7);
tb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Testing.class));
//sakht afzari button
Button sa=(Button) findViewById(R.id.button9);
sa.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Sakhtafzari.class));
//imei
Button imei = (Button) findViewById(R.id.button10);
imei.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Imei.class));
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我已将所有类添加到清单中,但我什么都没得到。为什么呢?
答案 0 :(得分:1)
你把所有东西放在第一个onClickListener
内,你不应该这样做。试试这个:
Button fr = (Button) findViewById(R.id.button2);
fr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Factoryreset.class));
}
}
// end onClickListener
// second button
Button pe = (Button) findViewById(R.id.button5);
pe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Endcallpower.class));
}
}
// end second onclicklistener
// etc.
在您的代码中,您使用onClickListener
方法拥有多个视图。也许你应该这样做,最好从几个视图处理onclick事件:
// implement an onClickListener method
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find ids (for all your views)
Button ceo = (Button) findViewById(R.id.codebutton);
Button fr = (Button) findViewById(R.id.button2);
Button pe = (Button) findViewById(R.id.button5);
// etc.
// then set the implementation (for all your views)
ceo.setOnClickListener(this);
fr.setOnClickListener(this);
pe.setOnClickListener(this);
// etc.
}
// call onClick method
@Override
public void onClick(View v) {
// receive the button clicked by its id
switch(v.getId()) {
// select the id
case R.id.codebutton:
// do some stuff
break;
case R.id.button2:
// do some stuff
break;
case R.id.button5:
// do some stuff
break;
// etc.
}
}
}