【问题标题】:Setting a button enabled设置启用按钮
【发布时间】:2013-02-13 14:07:45
【问题描述】:

我有一个有 2 个按钮的主页,按钮 1 已启用,按钮 2 已禁用。 button1 将打开有 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" />

【问题讨论】:

    标签: android


    【解决方案1】:

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

    【讨论】:

    • 完全不同意 - XML 非常适合初始化诸如启用之类的值,并且在代码中是您可以在布局后更改它们的唯一方法,所以我不确定您在做什么关于!
    【解决方案2】:

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

    要解决您的问题,请查看 this 文章,该文章涵盖 Intents、intent extras(传递数据 - 这将允许您设置 Main Activity 可用于确定是否启用按钮的属性)和意图标志(操纵类的实例如何运行 - 一种选择是使用FLAG_ACTIVITY_REORDER_TO_FRONT,它采用任何现有实例并将其放在堆栈的顶部),或者只是搜索有关标志和附加功能的信息,他们应该向您展示您需要了解的内容。

    【讨论】:

    • 有人有解决办法吗?
    • 我几乎给了你一个。我不会为你编写你的代码 - 你会学到更多我自己研究它,相信我,这会更令人满意
    【解决方案3】:

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

    【讨论】:

    • 我应该怎么做才能防止它被禁用?
    • 尝试调用 setContentView(R.layout.activity_enable);启用按钮后。
    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 2016-02-14
    • 2012-10-16
    • 1970-01-01
    相关资源
    最近更新 更多