以下代码无法在另一个活动中设置窗口小部件可见

时间:2015-08-12 14:22:10

标签: java android button android-activity preferences

我做了一些编码,使按钮在另一个活动中可见,但它仍然无效,我有三个类: menu.classlevelone.classleveltwo.class 这是menu.xml

中的以下数据
<Button
    android:id="@+id/f1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button1" />

<ImageView
    android:id="@+id/f2lock"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/levellocked" />

<Button
    android:id="@+id/f2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button2"
    android:visibility="gone" />

然后是menu.class

中的代码
        f1 =(Button)findViewById(R.id.f1);      
        f1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v){
           // TODO Auto-generated method stub
        Intent intent = new Intent ();
        intent.setClassName ("com.example.application", "com.example.application.levelone");
        startActivityForResult (intent, 0);              
            }             
      });   
}
    public void onActivityResult (int requestCode, int resultCode, Intent intent) {
        super.onActivityResult (requestCode, resultCode, intent);

    f2=(Button)findViewById(R.id.f2);
    f2.setEnabled(false);
    f2lock=(ImageView)findViewById(R.id.f2lock);

        switch (resultCode) {
        case 11: f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);               
                            }                   

    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    boolean levelTwoUnlocked = preferences.getBoolean("f2", false);

    if(levelTwoUnlocked){
        f2.setVisibility(View.VISIBLE);
        f2lock.setVisibility(View.GONE);
        }
        else {
        f2.setVisibility(View.GONE);
        f2lock.setVisibility(View.VISIBLE);
        } 


    f2.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v){
        Intent intent = new Intent ();
        intent.setClassName ("com.example.application", "com.example.application.leveltwo");
        startActivityForResult (intent, 0);              
            }             
      });       
}

levelone.class我放了

public void onClick(View v){
                  setResult (11);
                  finish();          
              }
           }); 

结果将发送到menu.class以使按钮可见并打开leveltwo.class

            switch (resultCode) {
            case 11: f2.setVisibility(View.VISIBLE);
                     f2lock.setVisibility(View.GONE);               
                                }

menu.class中没有任何内容发生,f2按钮仍然不可见

我的代码没有功能或首选项代码是错误的吗?

1 个答案:

答案 0 :(得分:0)

我刚刚在你的onActivityResult()函数中注意到你先设置了可见性,然后在布局中初始化这些变量试试这个

public void onActivityResult (int requestCode, int resultCode, Intent intent) {
    super.onActivityResult (requestCode, resultCode, intent);    

    //First initialize these variables and then use setVisbility function
    f2=(Button)findViewById(R.id.f2);
    f2.setEnabled(false);
    f2lock=(ImageView)findViewById(R.id.f2lock);

    switch (resultCode) {
        case 11: f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);               
                        }    
    //Rest of your code
}