简体   繁体   中英

onClickListener for a button text to change based on edit text

I have the following code but it changes the button text to empty. I'm basically trying to change the the button's text when it clicked to whatever the user types into the edit text field I have. Everything seems to work, however, when I click on either buttons I have the button's text changes to empty/null and since i'm using wrap content it shrinks the size of the button too.

activity_main.xml

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="10dp"
            android:text="Name: Balkar Rana"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:textColor="#FFFF00"
            android:textSize="20dp"
            android:textStyle="bold"
            />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textColor="#FF69B4"
            android:text="Username: "
            android:textSize="20dp"/>

        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:textColor="#000000"
            android:textSize="20dp"
            android:id="@+id/username"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textColor="#63ea1f"
            android:text="Password: "
            android:textSize="20dp"/>

        <EditText
            android:inputType="textPassword"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:textColor="#000000"
            android:textSize="20dp"
            android:id="@+id/password"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="25dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:text="Username"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#FF69B4"
            android:id="@+id/userButton"
            android:onClick="onClick"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:text="Password"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#63ea1f"
            android:id="@+id/passButton"
            android:onClick="onClick"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:text="USER-PASS"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#FF69B4"
            android:id="@+id/userPassButton"
            android:onClick="onClick"/>

    </LinearLayout>


    </LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java

    package com.example.balkarrana.lab2;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity{

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

        EditText usernameInput = (EditText)findViewById(R.id.username);
        final String userText = usernameInput.getText().toString();
        final Button userButtonVariable = (Button)findViewById(R.id.userButton);

        EditText passwordInput = (EditText)findViewById(R.id.username);
        final String passText = passwordInput.getText().toString();
        final Button passButtonVariable = (Button)findViewById(R.id.passButton);

        final String userPassText = userText + passText;
        final Button userPassButtonVariable = (Button)findViewById(R.id.userPassButton);

        userButtonVariable.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                userButtonVariable.setText(userText);
            }
        });

        passButtonVariable.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                passButtonVariable.setText(passText);
            }
        });

        userPassButtonVariable.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                userPassButtonVariable.setText(userPassText);
            }
        });
    }

    }

You are getting the value of the EditText within your onCreate when the app is first started and they will be blank. You need to get the current value within the onClick and then set it. Here's how I'd do it for the userButton.

...
public class MainActivity extends AppCompatActivity{

EditText usernameInput;
Button userButtonVariable;

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

        usernameInput = (EditText)findViewById(R.id.username);
        userButtonVariable = (Button)findViewById(R.id.userButton);

        userButtonVariable.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String userText = usernameInput.getText().toString();
                userButtonVariable.setText(userText);
    }
});

To stop a button changing size don't set the width to be wrap_content.

Try match_parent and then use margins.

<Button
       android:layout_width="match_parent"
...
</Button>

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