简体   繁体   English

双击Android按钮

[英]Double Click on Button Android

First off this question has been asked multiple times, however , none of these questions have been answered to any extent. 首先,这个问题已经被问过多次, 但是 ,这些问题都没有得到任何回答。 I have one example that works in the main activity class: 我有一个在主要活动类中工作的示例:

final Button button = (Button) findViewById(R.id.viewcatalog);
button.setFocusable(true);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        setContentView(R.layout.find_item);
    }
 });

But all of my other attempts to replicate this in sequential pages has resulted in failure. 但是我在顺序页面中重复进行此操作的所有其他尝试均导致失败。 I know the reason that they won't work the same way is that my buttons are instantiated in other classes and not in the host class. 我知道它们无法以相同方式工作的原因是我的按钮在其他类而不是在宿主类中实例化。 What is the correct way to fix this error? 解决此错误的正确方法是什么?

The method that doesn't work for reference: 该方法不适用于参考:

public void OnClickSearch(View view) {
    final Button button = (Button) findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText text = (EditText)findViewById(R.id.editText);
            String value = text.getText().toString();
            setContentView(R.layout.search_results);
        }
    });
}

It sounds like you are mis-understanding how the UI works in Android. 听起来您误解了UI在Android中的工作方式。 It is not normally expected that you will change an Activity's view on the fly as your are doing in your OnClickListener. 通常不希望您像在OnClickListener中一样动态地更改Activity的视图。

Instead, you should do one of two things. 相反,您应该执行以下两项操作之一。 Either switch to a new Activity, using an Intent and the Activity's startActivity method, or use Fragments , and replace a Fragment in your Activity with a new Fragment. 使用Intent和Activity的startActivity方法切换到新的Activity,或者使用Fragments ,然后用新的Fragment替换Activity中的Fragment。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM