是否可以将EditText的倍数设置为1 Textview并自动显示而无需单击任何按钮。这是XML示例。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnim"
android:layout_marginTop="7dp"
android:text="Name :"
android:textSize="18sp" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnama"
android:layout_marginTop="25dp"
android:text="Spesification"
android:textSize="18sp" />
<EditText
android:id="@+id/spesification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
>
</EditText>
<TextView
android:id="@+id/TextView Output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnama"
android:layout_marginTop="25dp"
android:text="Output"
android:textSize="18sp" />
</LinearLayout>
我希望数据在TextViewOutput中显示为“名称+规范”。同样,当我键入内容时,只要我同时输入Both EditText
,输出就会自动更改为答案 0 :(得分:0)
是的,您必须使用addTextChangedListener
用户,并遵循以下代码
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String editValue = editText.getText().toString();
if(editValue.lenght!=0){
String textValue = yourTextView.getText().toString();
int val = Integer.parse(textValue);
int edVal = Integer.parse(editValue);
int result = edVal * val;
resultText.setText(result);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
希望这会对您有所帮助。
答案 1 :(得分:0)
很难在同一textView中维护两个不同的addOnTextChangeListerner's
输出,因此最好使用-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnama"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_Output1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginRight="2dp"
android:text="Output"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_Output2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text=""
android:textSize="18sp" />
</LinearLayout>
并在两个 editText 上应用addTextChangedListener
,并在相应的 TextViews 中设置其输出,如下所示
final EditText nameEditText = (EditText) findViewById(R.id.name);
final EditText spesificationEditText = (EditText) findViewById(R.id.spesification);
final TextView textViewOutput1 = (TextView) findViewById(R.id.textView_Output1);
final TextView textViewOutput2 = (TextView) findViewById(R.id.textView_Output2);
nameEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String name = nameEditText.getText().toString();
if(name!=null){
textViewOutput1.setText(name);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
spesificationEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String specification = spesificationEditText.getText().toString();
if(specification!=null){
textViewOutput2.setText(specification);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
答案 2 :(得分:0)
谢谢你帮助我。你们所有人都是一个了不起的人。这是代码
EditText的前2个值或更多显示在1个Textview中
TextView output.setText(Name.getText().toString()+"-"+Spesicification.getText().toString()"-"+MoreOuput.getText().toString());
对于自动文本,您使用TextWatcher是正确的
Name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String name = NamaAlat.getText().toString();
if(name!=null){
TextView output.setText(Name.getText().toString()+"-"+Spesicification.getText().toString()"-"+MoreOuput.getText().toString());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Spesification.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String spek = NamaAlat.getText().toString();
if(spek!=null){
TextView output.setText(Name.getText().toString()+"-"+Spesicification.getText().toString()"-"+MoreOuput.getText().toString());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
非常感谢。