简体   繁体   中英

Using a button with the code inside of a different class

I am trying to make an app and I have the code for a button inside of a different class. When I start my app and click the first button it brings me to a different layout where the button is located. But when I click this button it doesn't do anything, just the little click down animation.

First Button Code:

public class TextAdd extends AppCompatActivity {
public static   EditText Text;
public static   Button Set;
public static   String[] Checkagainst = new String[1000];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.Text_Checker);
    Text = (EditText) findViewById(R.id.LPN);
    Set = (Button) findViewById(R.id.Set);

    Set.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String Text_Value = Text.getText().toString();
            if (!Arrays.asList(Checkagainst).contains(Text_Value) && Text_Value.length() >= 1 && Text_Value.length() <= 7) {
               setContentView(R.layout.add);


                for (int i = 0; i < Checkagainst.length; i++) {
                    if (Checkagainst[i] == null) {
                        Checkagainst[i] = Text_Value;
                        break;
                    }
                }

            } else if (Arrays.asList(Checkagainst).contains(Text_Value) && Text_Value.length() >= 1 && Text_Value.length() <= 7) {
                setContentView(R.layout.have);

            }
        }
    });

}
}

Second Button Code:

public class Have extends AppCompatActivity {

private Button HaveBack;
private TextView Have;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.have);
    HaveBack = (Button) findViewById(R.id.HaveBack);
    Have= (TextView) findViewById(R.id.Have);
    String Text_Value= TextAdd.License.getText().toString();
    String Extra = Text_Value + " is already part of Your license plates";
    Have.setText(Extra);
    HaveBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.Text_Checker);
        }
    });
}
}

Does anyone know what is wrong? If so can you please help me.

You should use setContentView() only once in your onCreate() method. Calling it multiple times is not correct. If you want to show a small layout above your current layout, you should use a Dialog and if you want to show a completely different layout above everything, you have to use Intents to go to another activity and do the rest of the work in that one.

besides, use lowercase letters at start of your variables' and objects' names and start Class names with Uppercase letters. That's the standard for knowing what is a class and what is an object. eg

Button firstButton, secondButton;

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