0

2 つのボタンがあるメイン ページがあり、button1 が有効で、button2 が無効になっています。button1 は、button3 という名前のボタンが 1 つある 2 番目のページを開きます。button3 はメイン ページに戻り、button2 を有効にする必要があります。問題は、button2 が短期間有効になり、その後無効 (網掛け) に戻ることです。

メインページ。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Enable extends Activity {

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


    Button page1 = (Button) findViewById(R.id.button1);
    page1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            Intent myIntent = new Intent(view.getContext(), p2.class);
            startActivityForResult(myIntent, 0);

        }
    });

    Button page2 = (Button) findViewById(R.id.button2);

    page1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            Intent myIntent = new Intent(view.getContext(), p2.class);
            startActivityForResult(myIntent, 0);

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_enable, menu);
    return true;
}

}

2ページ目

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class p2 extends Activity {

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

    Button page1 = (Button) findViewById(R.id.button3);
    page1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            Intent myIntent = new Intent(view.getContext(), Enable.class);
            setContentView(R.layout.activity_enable);
            Button a = (Button) findViewById(R.id.button2);
            a.setEnabled(true);
            startActivityForResult(myIntent, 0);

        }
    });

}
}

メインページのXML

<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"
tools:context=".Enable" >

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="104dp"
    android:layout_marginTop="32dp"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="46dp"
    android:enabled="false"
    android:text="Button" />

ページ 2 の XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

4

3 に答える 3

1

XML を介してボタンの動作を制御する代わりに、アクティビティ コードで機能の有効化と無効化を制御します。これが役立つことを願っています

于 2013-02-13T14:18:29.737 に答える
0

あなたの問題は、インテントの仕組みにあります。2 番目のアクティビティが行っているのは、最初のアクティビティのレイアウトにアクセスすることです (現在は密結合になっているため、これは悪い習慣です) が、目的は Main アクティビティを再作成することです。つまり、onCreate メソッドが再度呼び出され、ボタンが再作成 - 有効なプロパティを false に設定します。

問題を解決するには、インテント、インテント エクストラ (データを渡すため - メイン アクティビティがボタンを有効にするかどうかを決定するために使用できるプロパティを設定できるようにするため)、インテント フラグについて説明しているこの記事をご覧ください。 (クラスのインスタンスがどのように動作するかを操作するFLAG_ACTIVITY_REORDER_TO_FRONTには、既存のインスタンスを取得してスタックの一番上に置くオプションを使用することもできます)、または単にフラグとエクストラに関する情報を検索すると、知っておくべきことが表示されます。 .

于 2013-02-13T14:15:58.310 に答える
0

インテントは再びアクティビティを開始し、アクティビティはボタン 2 を無効に設定する onCreate メソッドを呼び出します。

于 2013-02-13T14:16:24.253 に答える