简体   繁体   中英

Call EditText value in different file class java Android

I'm learning to use the external class. This project is just an example. I have a class that extends Activity and in this class I want to access my Login button. And the Login Button will run the external class that implements OnClickListener in OnClickListenerLogin().

In OnClickListenerLogin I want to get EditText value in Login.xml. But whenever I call it, the value returns "".

What's missing from my code and what is the right code for my OnClickListenerLogin so I can get the EditText value in Login.xml?

Login.xml

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    tools:context="com.example.phonekiosk.LoginActivity" >

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">

        <!-- Login Form -->
        <LinearLayout 
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dip">
            <!-- Email Label -->
            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Username"/>
            <EditText 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:layout_marginBottom="20dip"
                android:singleLine="true"
                android:id="@+id/txtUsername"/>
            <!-- Password Label -->
            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Password"/>
            <EditText 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:singleLine="true"
                android:password="true"
                android:id="@+id/txtPassword"/>
            <!-- Login Button -->
            <Button
                android:id="@+id/btnLogin"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:text="Login" />
        </LinearLayout>
        <!-- Login Form Ends -->
    </RelativeLayout>
</ScrollView>

PhoneKiosk.java

package com.example.phonekiosk;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
    Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new OnClickListenerLogin());
    }
}

OnClickListenerLogin.java

package com.example.phonekiosk;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.EditText;
import android.widget.Toast;

public class OnClickListenerLogin implements OnClickListener {

    @Override
    public void onClick(View view) {
        final Context context = view.getContext();

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View formLogin = inflater.inflate(R.layout.login, null);

        final EditText txtUsername = (EditText) formLogin.findViewById(R.id.txtUsername);
        final EditText txtPassword = (EditText) formLogin.findViewById(R.id.txtPassword);

        String username = txtUsername.getText().toString();
        String password = txtPassword.getText().toString();

        Log.d("test", username + "-" + password);
    }
}

You are not using the same view as that created by LoginActivity. Your code that inflates the view in onClick need to be removed.

You can pass the reference to EditText of user name & password to your OnClickListenerLogin by implementing a constructor for it.

public class OnClickListenerLogin implements OnClickListener {

        private EditText txtUsername;
        private EditText txtPassword; 

        public OnClickListenerLogin (EditText userEditText, EditText passwordEditText) {
            this.txtUsername = userEditText;
            this.txtPassword = passwordEditText;
        }

       @Override
       public void onClick(View view) {
             String username = txtUsername.getText().toString();
             String password = txtPassword.getText().toString();

             Log.d("test", username + "-" + password);
       }
 }

In the login activity, you can do following

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(
      new OnClickListenerLogin(
          (EditText) findViewById(R.id.txtUsername), 
          (EditText) findViewById(R.id.txtPassword));
}

Disclaimer : I have entered the code directly here, not checked for compilation issue due to typo. I hope you get the essence of the solution

no need of external class here

you can simply set onClickListener to your login button and perfrom your action in onClick method.

public class LoginActivity extends Activity {
Button btnLogin;
EditText txtUsername,txtPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    txtUsername = (EditText) findViewById(R.id.txtUsername);
    txtPassword = (EditText) findViewById(R.id.txtPassword);

    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

           String username = txtUsername.getText().toString();
            String password = txtPassword.getText().toString();

            // your action here

        }
    });
}

}

One solution would be to simple make the onClickListener a inner class of your Login Class

    public class LoginActivity extends Activity {
        Button btnLogin;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
              ............
        }

//inner class
 class OnClickListenerLogin implements OnClickListener {
                 .........
                  }

}//Login class ends

Inner classes exist primarily to solve these kind of problems , try reading about them .

this is very simple so you should proceed like below for getting the value from edittext.

public class LoginActivity extends Activity implements OnClickListener{
Button btnLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
        final EditText txtUsername = (EditText) formLogin.findViewById(R.id.txtUsername);
    final EditText txtPassword = (EditText) formLogin.findViewById(R.id.txtPassword)
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new OnClickListenerLogin());



}

public click(View view) {

    String username = txtUsername.getText().toString();
    String password = txtPassword.getText().toString();

    Log.d("test", username + "-" + password);

} }

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