简体   繁体   中英

Java Activity TextEdit input not printing when I click the appropriate button

Here is my activity's code

public class MainActivity extends Activity {
    String height, weight;
    String dob, dov;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            enterClicked();
        }
    });
}

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

public void enterClicked()
{
    height = ((EditText) findViewById(R.id.editText1)).getText().toString();
    weight = ((EditText) findViewById(R.id.editText2)).getText().toString();
    System.out.println( height + " " + weight );
}
}

When I run this code on the emulator I want it to take in the text I input and when I click the button I want it to retrieve the text and print it in the log. It doesn't currently print. I don't have any pre-compile errors and it appears my links to my EditTexts are successful. What might be the issue?

you can't use System.out.println for android apps. See this question: Why doesn't "System.out.println" work in Android?

Consider using Log for this purpose. It provides logging options for a number of scenarios. You can direct and segregate log messages based on their nature. Example:

~ Debug:

Log.d(String tag, String msg)

~ Info Log.i(String tag, String msg)

.... ....

In your case, you can use Log.i as such:

Log.i("MainActivity", height + " " + weight);

The output will be available in logcat under the info view.

You can read up on the options that Log class provides here: Link .

You can use Log or Toast . These are the best ways:

  1. Log.i("Display TextView check", height + " " + weight);

  2. Toast.makeText(getApplicationContext(), "Text check:" + height + " " + weight, Toast.LENGTH_LONG).show();

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