简体   繁体   English

将按钮与不同类中的代码一起使用

[英]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. 您只能在onCreate()方法中使用setContentView()一次。 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. 如果要在当前布局上方显示一个较小的布局,则应使用一个对话框,如果要在所有内容上方显示一个完全不同的布局,则必须使用Intent转到另一个活动,并在其中进行其余的工作。一。

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;

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

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