使用多个按钮来启动不同的活动的Android [英] Using multiple button to start different activities Android
问题描述
我有3个活动。主要业务是在主屏幕上有两个按钮,每个按钮应显示2上次活动之一。我尝试了无论是在意图模式和开关模式多个按钮许多方法;但我仍然可以有两个工作按钮。第一个按钮开始其链接活动没有任何问题,但第二个按钮仍然不会露面。这里是java code:
I have 3 activities. Main activity which is the home screen with two buttons, each button should show up one of the 2 last activity. I tried many methods for multiple buttons both in intent mode and switch mode; but I still can have two working button. The first button starts its linked activity without any problem, but the second button still won't show up. Here is the java code:
package com.live.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
Button button01;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WoneWideo.class);
startActivity(intent);
}
});
}
public void addListenerOnButton2() {
final Context context = this;
button01 = (Button) findViewById(R.id.button01);
button01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
});
}
}
主要布局文件的内容:
The main layout file content:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
..........................
..............................
<Button
android:id="@+id/buttonUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/live_button"
android:onClick="onClick"/>
<Button
android:id="@+id/button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/vod_button"
android:onClick="onClick"/>
</LinearLayout>
另外我有所有这些活动/ manifest文件中声明的类。
Also I have all these activities/class declared in the Manifest file.
推荐答案
您忘记调用 addListenerOnButton2()
。总之你可以让你code简单
You forget to call addListenerOnButton2()
. Anyhow You can make your code simple
试试这个:
Button btn1,btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.buttonUrl);
btn2 = (Button)findViewById(R.id.button01);
}
public void onClick(View v){
if(v.getId() == R.id.buttonUrl){
Intent intent = new Intent(context, WoneWideo.class);
startActivity(intent);
}else if(v.getId() == R.id.button01){
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
}
这篇关于使用多个按钮来启动不同的活动的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!