而且我不知道可以做我想做的事..(对不起我的英语)
<TableRow>
<Button
android:id="@+id/button_left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="moveLeft"
android:layout_column="1"
android:background="@drawable/bt_left"/>
<com.pauza.simpletetris.MyView
android:id="@+id/my_view"
android:layout_width="250dp"
android:layout_height="490dp"
android:layout_gravity="center_horizontal"
android:layout_column="2"
android:orientation="vertical"/>
我从这里调用 MyView,但在 MyView 中 Button_Left 不存在。 游戏 Activity :
public class GameActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button bt_right=(Button)findViewById(R.id.button_right);
Button bt_left=(Button)findViewById(R.id.button_left);
bt_right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
moveRight();
}
});
bt_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
moveLeft();
}
});
}
protected void moveLeft() {
// TODO Auto-generated method stub
Toast.makeText(this, "You clicked left", Toast.LENGTH_LONG) .show();
}
private void moveRight() {
//call function from com.pauza.simpletetris.MyView <-- possible????
}
希望你明白我的意思,谢谢你所做的一切。
最佳答案
为了能够从 XML 中设置 onClick
回调方法(即使出于可维护性原因 I don't recommend it),这些方法需要公开,命名必须完全匹配,并且需要额外的参数: View
。因此,将 moveRight 和 moveLeft 更改为:
public void moveLeft(View v) {
// TODO Auto-generated method stub
Toast.makeText(this, "You clicked left", Toast.LENGTH_LONG) .show();
}
public void moveRight(View v) {
//call function from com.pauza.simpletetris.MyView <-- possible????
}
在 xml 布局中,您将有 android:onClick="moveLeft"
resp android:onClick="moveRight"
。这样您就不需要调用 setOnClickListener
。
关于android - 从另一个布局调用不同的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18653705/