0

New_Game类

imageView1.setOnClickListener(new View.OnClickListener(){

   public void onClick(View v) {
       Intent myIntent = new Intent(v.getContext(), Image_Quiz.class);>

       Bundle extras = new Bundle();
       extras.putBoolean("showButton1", true);
       myIntent.putExtras(extras);
       startActivityForResult(myIntent, 0);
       Image_Quiz.button1.setVisibility(View.VISIBLE);
   }

});

Image_Quiz 类

protected void onCreate(Bundle savedInstanceState) {

   this.setContentView(R.layout.image_quiz);
   Bundle extras = this.getIntent().getExtras();
   Boolean showButton = extras.getBoolean("showButton1");
   if (showButton){
       this.findViewById(R.Image_Quiz.button1).setVisibility(View.VISIBLE);
   }
   super.onCreate(savedInstanceState);

?xml 版本=“1.0”编码=“utf-8”?

   RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

     EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"

     TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Back"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"

      TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Main Menu"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"

      Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Buton 1"
        android:id="@+id/button"
        android:layout_above="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="22dp"

     EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"

      Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Buton 2"
        android:id="@+id/button2"
        android:layout_alignBottom="@+id/button"
        android:layout_alignRight="@+id/button"
        android:layout_alignLeft="@+id/button"
        android:layout_alignTop="@+id/button"
        
   RelativeLayout

在 Image_Quiz 类

     button1 = (Button)findViewById(R.id.button);

Image_Quiz 是我按下 imageView1 时从这个类切换的类。

当我按下 imageView1 时,它会打开 Image_Quiz 类,但按钮不可见。

4

1 回答 1

2

您应该将参数传递给您正在调用的活动,例如:

imageView1.setOnClickListener(new View.OnClickListener(){

   public void onClick(View v) {
       Intent myIntent = new Intent(v.getContext(), Image_Quiz.class);
       Bundle extras = new Bundle();
       extras.putBoolean("showButton1", true);
       myIntent.putExtras(extras);
       startActivityForResult(myIntent, 0);
   }
});

在你的 Image_Quiz 里面:

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.setContentView(R.layout.image_quiz);
    Bundle extras = this.getIntent().getExtras();
    Boolean showButton = extras.getBoolean("showButton1");
    Button button1 = (Button)findViewById(R.id.button);
    if (showButton){
        button1.setVisibility(View.VISIBLE);
    } else {
        //...you know...
    }
    super.onCreate(savedInstanceState);
}
于 2013-06-07T16:36:23.920 回答