我是 android 新手,我刚刚了解了活动,我决定创建一些。
我的 MainActivity.java 是,
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.button1);
a.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, WidgetIntro.class));
Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Alarm.class));
Button c = (Button) findViewById(R.id.button4);
c.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Sms.class));
Button d = (Button) findViewById(R.id.button6);
d.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Call.class));
Button e = (Button) findViewById(R.id.button3);
e.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Notes.class));
Button f = (Button) findViewById(R.id.button5);
f.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Cbdata.class));
}
});
}
});
}
});
}
});
}
});
虽然这没有错误,并且我已经为每个按钮创建了一些文本来祝酒,但是当我在手机上安装我的应用程序时,我只能按照我给它们编号的顺序访问按钮,即“位置小部件” button 我的按钮 id 是 button1 所以除了这个按钮我可以选择任何其他按钮,一旦我选择了这个按钮,我只能选择按钮 2 而不能选择任何其他按钮,为什么会发生这种情况?
我的 activity_main.xml 代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="26dp"
android:text="About Us" />
<Button
android:id="@+id/button3"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button7"
android:layout_alignParentRight="true"
android:layout_marginBottom="36dp"
android:text="Note Remainder" />
<Button
android:id="@+id/button2"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignParentLeft="true"
android:text="Alarm" />
<Button
android:id="@+id/button4"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="40dp"
android:text="SMS" />
<Button
android:id="@+id/button6"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_alignParentRight="true"
android:text="Call" />
<Button
android:id="@+id/button1"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button6"
android:layout_alignParentRight="true"
android:layout_marginBottom="37dp"
android:text="Location Widget" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Hello User, Welcome to Mobile location Info! You Choose from the following options :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button5"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:text="Save my locations" />
</RelativeLayout>
}
});
}
}