设置按钮已启用

时间:2013-02-13 14:07:46

标签: android

我有一个主页面有2个按钮,按钮1已启用,按钮2已禁用。 button1将打开第2页,其中有1个名为button3的按钮。 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;
}

}

第二页

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" />

3 个答案:

答案 0 :(得分:1)

不是通过XML控制按钮的行为,而是控制活动代码中的启用和禁用功能。希望这会有所帮助

答案 1 :(得分:0)

你的问题在于Intents的工作方式。你的第二个活动是访问第一个活动的布局(这是不好的做法,因为它们现在紧密耦合),但目的是重新创建Main活动 - 这意味着再次调用onCreate方法,按钮是重新创建 - 将enabled属性设置为false。

要解决您的问题,请查看this文章,其中包含Intents,intent extras(传递数据 - 这将允许您设置主要活动可用于确定是否启用的属性按钮)和意图标志(用于操纵类的实例如何操作 - 一个选项是使用FLAG_ACTIVITY_REORDER_TO_FRONT,它接受​​任何现有实例并将其放在堆栈的顶部),或者仅搜索有关标志的信息他们应该向你展示你需要知道的内容。

答案 2 :(得分:0)

Intent再次启动活动,活动调用onCreate方法,该方法将Button 2设置为禁用。

相关问题