简体   繁体   中英

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. It is not normally expected that you will change an Activity's view on the fly as your are doing in your OnClickListener.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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