我想知道你们是否可以在我的应用上帮助我。对于那些我缺少的人来说,这真的很容易。我正在尝试链接一个布局上的按钮以导航到其他布局。这六个按钮应该分为六种不同的布局......
按钮7应转到布局Number7 我已经做了按钮1,它适用于number1。
这是布局的屏幕截图,这是我的main.java的代码:http://imgur.com/zLJ3cdE
package com.example.isthisyourluckyday;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Main.this, Number1.class));
}
});
}
}
另外...... ..这里是number7.java,应链接到number7布局
package com.example.isthisyourluckyday;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Number7 extends Activity {
Button button7;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.number7);
}}
如果你们能帮助我,我真的很感激。
答案 0 :(得分:0)
如果第一个按钮有效,那么其他按钮的问题在哪里?
下面是所有代码:
public class Main extends Activity implements View.OnClickListener {
private final Button button1;
private final Button button2;
private final Button button3;
private final Button button4;
private final Button button5;
private final Button button6;
private final Button button7;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.equals(button1))
startActivity(new Intent(Main.this, Number1.class));
else if(v.equals(button2))
startActivity(new Intent(Main.this, Number2.class));
else if(v.equals(button3))
startActivity(new Intent(Main.this, Number3.class));
else if(v.equals(button4))
startActivity(new Intent(Main.this, Number4.class));
else if(v.equals(button5))
startActivity(new Intent(Main.this, Number5.class));
else if(v.equals(button6))
startActivity(new Intent(Main.this, Number6.class));
else if(v.equals(button7))
startActivity(new Intent(Main.this, Number7.class));
}
}
Main类可以实现OnClickListener只有一个onClick()函数。
答案 1 :(得分:0)
我没有正确测试,但你明白了。它与其他答案类似,但我认为代码更清晰,重复次数更少。
package com.example.isthisyourluckyday;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements View.OnClickListener {
final Button[] buttons = new Button[7];
final Class[] classes = { Number1.class, Number3.class, Number3.class,
Number4.class, Number5.class, Number6.class, Number7.class };
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttons[0] = (Button) findViewById(R.id.button1);
buttons[1] = (Button) findViewById(R.id.button2);
buttons[2] = (Button) findViewById(R.id.button3);
buttons[3] = (Button) findViewById(R.id.button4);
buttons[4] = (Button) findViewById(R.id.button5);
buttons[5] = (Button) findViewById(R.id.button6);
buttons[6] = (Button) findViewById(R.id.button7);
for(Button b : buttons){
b.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
for(int i=0; i<buttons.length; i++){
if(buttons[i].equals(v)){
Intent intent = new Intent(Main.this, classes[i]);
startActivity(intent);
}
}
}
}
答案 2 :(得分:0)
当你开始这样的活动时 startActivity(new Intent(Main.this,Number1.class)); 你告诉Android开始Number1活动。因此,如果您想开始另一项活动,您只需做同样的事情:
final Button b7 = (Button) findViewById(R.id.button7);
b7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Number7.class));
}
});
这不是很优雅,但可以解决问题。
你也可以在每个按钮上定义一个“标签”,如
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:onClick="choose"/>
然后在你的活动中添加一个方法
public void choose(View view) {
String number = view.getTag().toString();
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.example.isthisyourluckyday", "com.example.isthisyourluckyday.Number"+number));
startActivity(intent);
}