簡體   English   中英

如何使用 Java 從另一個活動啟用按鈕?

[英]How do I enable a button from another activity using Java?

活動級別.xml

<Button
    android:id="@+id/level2"
    android:layout_width="100dp"
    android:layout_height="105dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginTop="200dp"
    android:layout_marginEnd="130dp"
    android:layout_marginRight="198dp"
    android:background="#FF0000"
    android:text="2"
    android:textSize="40sp"
    android:enabled="false" />

級別.java

public class Levels extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_levels);
    }
}

activity_level_one_result.xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Next Level"
    android:textAllCaps="false"
    android:onClick="nextLevel"/>

一級結果.java

public class LevelOneResult extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_one_result);
    }

    public void nextLevel(View view) {
        startActivity(new Intent(getApplicationContext(), Levels.class));
        // enable the button here
    }
}

我想從activity_levels.xml文件啟用級別 2 按鈕。 我希望通過在LevelOneResult.java使用 Java 來啟用它。 正如您在上面看到的,我在要放置代碼的位置添加了一個注釋部分。

在你的 LevelOneResult.java 中創建一個interface ,如下所示,我在代碼的注釋中解釋:

public class LevelOneResult extends AppCompatActivity {

    OnCompleteListener mListener;
    //create an listener
    public  interface OnCompleteListener {
         void onComplete(boolean enableOrNot);
    }
    //attach the listener in the activity
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            this.mListener = (OnCompleteListener)activity;
        }
        catch (final ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_one_result);


    }

    public void nextLevel(View view) {
        startActivity(new Intent(getApplicationContext(), Levels.class));
        // enable the button here

        here trigger the listener
        //true means enable
         mListener.onComplete(true);
    }
}

在你的Levels.java ,你需要實現interface ,從接口onComplete()獲取數據並做你的事情

public class Levels extends AppCompatActivity implements LevelOneResult.OnCompleteListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_levels);
    }

    public void onComplete(boolean enableOrNot ) {
        // after the action in LevelOne
        // the boolean trigger here..

        //here the boolean is true,which u set in levelOneJava
        if(enableOrNot){
            //then do your stuff here
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM