我正在尝试创建一个程序,该程序将用户文本输入并在textview中显示,同时根据已检查的单选按钮和/或复选框进行格式化。
这是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("");
}
}
}
答案 0 :(得分:1)
我猜你试图通过使用XML中的onClick属性来捕获EditText的点击次数。我不认为这适用于你要做的事情。您希望查找按键而不是点击次数。尝试在您的oncreate中的EditText上放置一个监听器。如下所示。
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;
}
});