Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我对我的代码进行了验证,但我想它还是不起作用_Android_Android Studio_Android Layout - Fatal编程技术网

Android 我对我的代码进行了验证,但我想它还是不起作用

Android 我对我的代码进行了验证,但我想它还是不起作用,android,android-studio,android-layout,Android,Android Studio,Android Layout,我已经编写了验证代码…但在执行应用程序时,验证不起作用。就像我单击按钮但没有输入详细信息时,它应该显示验证…但它会显示在下一页上 这是我的activity_main.xml代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln

我已经编写了验证代码…但在执行应用程序时,验证不起作用。就像我单击按钮但没有输入详细信息时,它应该显示验证…但它会显示在下一页上

这是我的activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ic_launcher_background"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/plain_text_input"
        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="142dp"
        android:layout_marginRight="142dp"
        android:layout_marginBottom="439dp"
        android:drawableLeft="@drawable/ic_action_username"
        android:fontFamily="sans-serif"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="USERNAME"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/txtusername"
        android:layout_width="293dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="41dp"
        android:layout_marginBottom="383dp"
        android:contextClickable="true"
        android:ems="10"
        android:hint="ENTER USERNAME"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="23dp"
        android:layout_marginLeft="23dp"
        android:layout_marginEnd="78dp"
        android:layout_marginRight="78dp"
        android:layout_marginBottom="325dp"
        android:drawableLeft="@drawable/ic_action_password"
        android:fontFamily="sans-serif"
        android:text="PASSWORD"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/txtpassword"
        android:layout_width="277dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="56dp"
        android:layout_marginBottom="263dp"
        android:hint="ENTER PASSWORD"
        android:inputType="textPassword"
        android:textSize="15dp" />

    <androidx.cardview.widget.CardView
        android:layout_width="348dp"
        android:layout_height="56dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="28dp"
        android:layout_marginBottom="170dp"
        app:cardBackgroundColor="@color/DeepPink"
        app:cardCornerRadius="30dp"
        app:cardElevation="10dp">

        <RelativeLayout
            android:layout_width="373dp"
            android:layout_height="73dp"
            android:layout_marginEnd="50dp"
            android:layout_marginBottom="100dp">

            <Button
                android:id="@+id/textView2"
                android:layout_width="124dp"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="147dp"
                android:layout_marginBottom="18dp"
                android:background="@android:color/transparent"
                android:freezesText="true"
                android:onClick="LOGIN"
                android:text="LOGIN "
                android:textColor="@color/Black"
                android:textSize="25dp" />
        </RelativeLayout>
    </androidx.cardview.widget.CardView>

    <Button
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="118dp"
        android:layout_marginRight="118dp"
        android:layout_marginBottom="66dp"
        android:background="@android:color/transparent"
        android:fontFamily="sans-serif"
        android:text="SIGN IN NOW"
        android:textAllCaps="true"
        android:textColor="@color/Black"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="106dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="73dp"
        android:layout_marginBottom="522dp"
        app:srcCompat="@drawable/ticket" />




</RelativeLayout> 

这是我的MainActivity.java代码

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

    EditText txtusername;
    EditText txtpassword;
    Button textView2;
    ToastManager toastManager;

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

        txtusername = (EditText) findViewById(R.id.txtusername);
        txtpassword = (EditText) findViewById(R.id.txtpassword);
        textView2 = (Button) findViewById(R.id.textView2);


        Button textView3 = (Button) findViewById(R.id.textView3);
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, NewUserActivity.class);
                startActivity(i);
            }
        });

        Button textView2 = (Button) findViewById(R.id.textView2);
        textView2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent u = new Intent(getApplicationContext(), MainActivity2.class);
                startActivity(u);
            }
        });

    }




    public void LOGIN(final View view) {
        textView2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                String username = txtusername.getText().toString().trim();
                String password = txtpassword.getText().toString().trim();

                ToastManager toastManager = new ToastManager(MainActivity.this);

                boolean isAtLeastOneEditTextNotEmpty = !username.isEmpty()
                        || !password.isEmpty();

                if (isAtLeastOneEditTextNotEmpty) {
                    if (username.isEmpty()) {
                        toastManager.addToast("ENTER USERNAME", ToastManager.Duration.LENGTH_SHORT);
                    } else if (!((username.length() > 6) && (username.length() < 15))) {
                        toastManager.addToast("USERNAME IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
                    }
                    if (password.isEmpty()) {
                        toastManager.addToast("ENTER PASSWORD", ToastManager.Duration.LENGTH_SHORT);
                    } else if (!((password.length() > 6) && (password.length() < 15))) {
                        toastManager.addToast("PASSWORD IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
                    }
                } else {
                    toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
                }

            }
        });

    }
}
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入androidx.appcompat.app.appcompat活动;
公共类MainActivity扩展了AppCompatActivity{
编辑文本txtusername;
编辑文本TXT密码;
按钮文本视图2;
ToastManager ToastManager;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtusername=(EditText)findViewById(R.id.txtusername);
txtpassword=(EditText)findViewById(R.id.txtpassword);
textView2=(按钮)findViewById(R.id.textView2);
按钮textView3=(按钮)findViewById(R.id.textView3);
textView3.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意图i=新意图(MainActivity.this、NewUserActivity.class);
星触觉(i);
}
});
按钮textView2=(按钮)findViewById(R.id.textView2);
textView2.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
意向u=新意向(getApplicationContext(),MainActivity2.class);
星触觉(u);
}
});
}
公共无效登录(最终视图){
textView2.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
字符串用户名=txtusername.getText().toString().trim();
字符串密码=txtpassword.getText().toString().trim();
ToastManager ToastManager=新的ToastManager(MainActivity.this);
布尔值IsAtleAstoNeeditTextNoteEmpty=!username.isEmpty()
||!password.isEmpty();
如果(isAtLeastOneEditTextNotEmpty){
if(username.isEmpty()){
toastManager.addToast(“输入用户名”,toastManager.Duration.LENGTH\u SHORT);
}否则如果(!((username.length()>6)和(&&(username.length()<15))){
toastManager.addToast(“用户名太短,必须在6-15个字符之间。”,toastManager.Duration.LENGTH_SHORT);
}
if(password.isEmpty()){
toastManager.addToast(“输入密码”,toastManager.Duration.LENGTH\u SHORT);
}如果(!((password.length()>6)和&(password.length()<15)),则为else{
toastManager.addToast(“密码太短,必须在6-15个字符之间。”,toastManager.Duration.LENGTH_SHORT);
}
}否则{
toastManager.addToast(“所有字段都是必需的”,toastManager.Duration.LENGTH\u SHORT);
}
}
});
}
}

有两种类型可调用操作 第一:是setOnClickListenr吗 二:(xml)onClick

您在代码中同时使用了这两种方法

但当您同时使用它们时,(setOnClickListenr)将被执行

因此,解决方案是删除(setOnClickListenr)或删除(android:onClick=“LOGIN”)

编辑:-如果选择获取(setOnClickListenr),请不要忘记复制验证代码,并像下面这样粘贴到其中

public class MainActivity extends AppCompatActivity {

EditText txtusername;
EditText txtpassword;
Button textView2;
ToastManager toastManager;

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

    txtusername = (EditText) findViewById(R.id.txtusername);
    txtpassword = (EditText) findViewById(R.id.txtpassword);
    textView2 = (Button) findViewById(R.id.textView2);


    Button textView3 = (Button) findViewById(R.id.textView3);
    textView3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(MainActivity.this, NewUserActivity.class);
            startActivity(i);
        }
    });

    textView2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String username = txtusername.getText().toString().trim();
            String password = txtpassword.getText().toString().trim();

            ToastManager toastManager = new ToastManager(MainActivity.this);

            boolean isAtLeastOneEditTextNotEmpty = !username.isEmpty()
                    || !password.isEmpty();

            if (isAtLeastOneEditTextNotEmpty) {
                if (username.isEmpty()) {
                    toastManager.addToast("ENTER USERNAME", ToastManager.Duration.LENGTH_SHORT);
                } else if (!((username.length() > 6) && (username.length() < 15))) {
                    toastManager.addToast("USERNAME IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
                }
                if (password.isEmpty()) {
                    toastManager.addToast("ENTER PASSWORD", ToastManager.Duration.LENGTH_SHORT);
                } else if (!((password.length() > 6) && (password.length() < 15))) {
                    toastManager.addToast("PASSWORD IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
                }
            } else {
                toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
            }
        }
    });


}
public类MainActivity扩展了AppCompatActivity{
编辑文本txtusername;
编辑文本TXT密码;
按钮文本视图2;
ToastManager ToastManager;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtusername=(EditText)findViewById(R.id.txtusername);
txtpassword=(EditText)findViewById(R.id.txtpassword);
textView2=(按钮)findViewById(R.id.textView2);
按钮textView3=(按钮)findViewById(R.id.textView3);
textView3.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意图i=新意图(MainActivity.this、NewUserActivity.class);
星触觉(i);
}
});
textView2.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
字符串用户名=txtusername.getText().toString().trim();
字符串密码=txtpassword.getText().toString().trim();
ToastManager ToastManager=新的ToastManager(MainActivity.this);
布尔值IsAtleAstoNeeditTextNoteEmpty=!username.isEmpty()
||!password.isEmpty();
如果(isAtLeastOneEditTextNotEmpty){
if(username.isEmpty()){
toastManager.addToast(“输入用户名”,toastManager.Duration.LENGTH\u SHORT);
}否则如果(!((username.length()>6)和(&&(username.length()<15))){
toastManager.addToast(“用户名太短,必须在6-15个字符之间。”,toastManager.Duration.LENGTH_SHORT);
}
if(password.isEmpty()){
toastManager.addToast(“输入密码”,toastManager.Duration.LENGTH\u SHORT);
}如果(!((password.length()>6)和&(password.length()<15)),则为else{
toastManager.addToast(“密码太短,必须在6-15个字符之间。”,toastManager.Duration.LENGTH_SHORT);
}
}否则{
toastManager.addToast(“所有字段都是必需的”,toastManager.Duration.LENGTH\u SHORT);