【发布时间】:2013-12-15 15:15:23
【问题描述】:
我创建了一个 main.xml,其中有 4 个按钮,然后,我创建了 Activity,我在其中为第一个按钮编写了一个 Intent 以打开一个新的 Activity,并且它起作用了。但是,我想为第二个按钮做同样的事情,但它不像第一个按钮那样做......我的答案是为什么?为什么我要改变做同样的动作?
这里是代码..
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/noua" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/zece" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/unspe" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/doispe" />
</LinearLayout>
MainActivity.java:
package com.orar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
//implement the OnClickListener interface
public class MainActivity extends Activity
implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button1);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.button1){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
从这里开始第二个按钮和问题...
//implement the OnClickListener interface
public class MainActivity extends Activity
implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button2);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.button2){
//define a new Intent for the second Activity
Intent intent2 = new Intent(this,ThirdActivity.class);
//start the second Activity
this.startActivity(intent2);
}
}
}
}
SecondActivity.java:
package com.orar;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.second);
}
}
ThirdActivity.java:
package com.orar;
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.third);
}
}
second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the second Activity"
/>
</LinearLayout>
第三个.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the third Activity"
/>
</LinearLayout>
【问题讨论】:
-
您的活动中没有第二个按钮的调用。您能否分享一下您尝试过的方法,并准确告诉我们哪些方面没有按预期工作?
-
我应该如何调用我的活动中的第二个按钮?我试图改变一些东西以使第二个按钮开始一个新的活动,但我没有..我应该在第一个按钮的代码中更改什么以对第二个按钮进行相同的操作?
标签: android button android-intent android-activity