I'm trying to make a program that takes user text input and displays it in a textview, while also being formatted depending on which radio button(s) and/or checkbox(es) has been checked.
Here's my entire code for mainactivity.java
package com.example.lab3_lefelhocz;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;
import android.graphics.Color;
public class MainActivity extends Activity {
TextView messageTextView;
EditText inputEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radioBlack:
if (checked)
// Text is black.
messageTextView.setTextColor(Color.BLACK);
break;
case R.id.radioBlue:
if (checked)
// Text is blue.
messageTextView.setTextColor(Color.BLUE);
break;
case R.id.radioRed:
if(checked)
// Text is red.
messageTextView.setTextColor(Color.RED);
break;
}
}
public void inputEditText(View view) {
if (inputEditText.getText().toString().equals("")) {
String editTextValue = inputEditText.getText().toString();
messageTextView.setText(editTextValue);
} else {
inputEditText.setText("");
messageTextView.setText("");
}
}
}
I'm guessing your trying to capture clicks to your EditText by using the onClick attribute in your XML. I don't think this works for what your tyring to do. You want to look for key presses rather than clicks. Try putting a listener on your EditText in your oncreate. Something like the following.
inputEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
inputEditText(v);
return true;
}
return false;
}
});
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.