简体   繁体   中英

Android Studio Java code

I'm trying to replace a part of the text after clicking the bottom but I can't make it work. When I click the bottom without entering a name the message should say "Hello Username", and if I enter a name the "Username" should be replace by the name. Here is the code so far. Thanks

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class HelloAndroid extends AppCompatActivity implements OnClickListener {

    private EditText name;
    private TextView display;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_android);
        name = (EditText) findViewById(R.id.name);
        display = (TextView) findViewById(R.id.display);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_hello_android, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        if (button == view) {
            String message = "Hello, Username " + name.getText().toString() + "!";

            if (button == view) {
                String username = "Hello " + name.getText().toString() + "";


                Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
                toast.show();
                display.setText(message);

            }
        }
    }
}
You are enable to do this because may be you are not getting click here . Here is my code change it .

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;
import android.widget.Toast;

import java.util.ArrayList;

public class HelloAndroid extends AppCompatActivity implements View.OnClickListener {



    private EditText name;
    private TextView display;
    private Button button;

    String message ;



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


        name = (EditText) findViewById(R.id.name);
        display = (TextView) findViewById(R.id.display);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button :
                if (name.getText().toString().isEmpty())
                {
                    message = "Hello username" ;

                }
                else {
                    message = "Hello "+name.getText().toString() ;

                }


            Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
                    toast.show();
                    display.setText(message);
                break;
        }
    }
}

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