【问题标题】:How to set a button, clickable, from another class?如何设置另一个类的可点击按钮?
【发布时间】:2013-12-16 11:15:11
【问题描述】:

我有 2 个类 MainActivityDialogBox,我的 *activity_main.xml* 中有一个按钮,最初是不可点击的。我希望每次用户输入正确的用户名和密码然后在我的对话框中单击 confrimsetPositiveButton 时使其可点击。

这是我的 MainActivity cpde:

public class MainActivity extends Activity {
    Button buttonSetting;

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

        buttonSetting = (Button) findViewById(R.id.buttonSettings);
    }

    public void ShowDialog(View view) {
        DialogBox dialogBox = new DialogBox();
        dialogBox.show(getFragmentManager(), "Dialog Box");
    }

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

这是我的对话框类:

public class DialogBox extends DialogFragment {
    private static String username = "admin";
    private static String password = "1234";
    String typeUsername;
    String typePassword;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final LayoutInflater layoutInflater = getActivity().getLayoutInflater();

        View custom = layoutInflater.inflate(R.layout.custom, null);
        final EditText editTextUsername = (EditText) custom.findViewById(R.id.editTextUsername);
        final EditText editTextPassword = (EditText) custom.findViewById(R.id.editTextPassword);            
        builder.setView(custom);

        typePassword = editTextPassword.getText().toString();
        typeUsername = editTextUsername.getText().toString();

        builder.setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getActivity(),
                        "Negative button was clicked",
                        Toast.LENGTH_SHORT).show();
                }
        });

        builder.setPositiveButton(R.string.confirm,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    typePassword = editTextPassword.getText().toString();
                    typeUsername = editTextUsername.getText().toString();

                    if (typePassword.equals(password)
                      && typeUsername.equals(username)) 
                    {
                        View activity = layoutInflater.inflate(R.layout.activity_main, null);
                        Button buttonSettings = (Button) activity.findViewById(R.id.buttonSettings);
                        buttonSettings.setEnabled(true);

                        Toast.makeText(getActivity(), "correct!",
                              Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "Wrong input",
                              Toast.LENGTH_SHORT).show();
                    }
                }
        });

        Dialog dialog = builder.create();
        return dialog;
    }
}

【问题讨论】:

标签: android android-ui android-button android-dialogfragment


【解决方案1】:

试试这个..

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

    buttonSetting = (Button) findViewById(R.id.buttonSettings);

// display the AlertDialog in your MainActivity
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final LayoutInflater layoutInflater = getActivity().getLayoutInflater();

View custom = layoutInflater.inflate(R.layout.custom, null);
final EditText editTextUsername = (EditText) custom.findViewById(R.id.editTextUsername);
final EditText editTextPassword = (EditText) custom.findViewById(R.id.editTextPassword);            
builder.setView(custom);


typePassword = editTextPassword.getText().toString();
typeUsername = editTextUsername.getText().toString();

builder.setNegativeButton(R.string.cancel,
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(getActivity(),
                        "Negative button was clicked",
                        Toast.LENGTH_SHORT).show();
            }
        });

builder.setPositiveButton(R.string.confirm,
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {


                typePassword = editTextPassword.getText().toString();
                typeUsername = editTextUsername.getText().toString();

                if (typePassword.equals(password)
                        && typeUsername.equals(username)) 
                {
                    //this will perform buttonSetting click function 
                    buttonSetting.performClick();
                    Toast.makeText(getActivity(),
                            "correct!",
                            Toast.LENGTH_SHORT).show();

                } 
                else 
                {
                    Toast.makeText(getActivity(),
                            "Wrong input",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

Dialog dialog = builder.create();

}

【讨论】:

    【解决方案2】:

    您应该始终将listen 更改为user inputs 并更改usernamepassword 框中的文本。但是这种方法很糟糕,当尝试随机输入文本的人可能很容易知道其正确的登录凭据时。你可以在android中设置text listeners

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多