我有一个编辑文字和按钮
EditText edit=(EditText) findViewById(R.id.edittext);
Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(context,edit.getText().toString(),Toast.LENGTH_SHORT).show();
edit.setText("hello");
}
}
但是当我点击按钮时,我可以通过“你好”(edittext的文本)获得祝酒词 但edittext是空的。这是一个错误吗?
(编辑)这是我的完整代码 find =(EditText)rowView.findViewById(R.id.txtfindings); finding.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(context)
.setTitle("Findings")
.setMultiChoiceItems(
CustomDialog.service.getFindingsArray(), // array of string ex(cd,hard drive,motherboard)
_selectionsFindings, // boolean array size=size of findingsarray
new DialogSelectionClickHandler()) // set click handler on checkbox
.setPositiveButton("OK",
new DialogButtonClickHandler() //set button click
).create()
.show();
}
});
public class DialogSelectionClickHandler implements
DialogInterface.OnMultiChoiceClickListener {
public void onClick(DialogInterface dialog, int clicked,boolean selected) {
} }
public class DialogButtonClickHandler implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int clicked) {
switch (clicked) {
case DialogInterface.BUTTON_POSITIVE:
printSelectedFinding();//call the function
break;
}
}
}
protected void printSelectedFinding() {
String textfinding = "";
ArrayList<String> values = new ArrayList<String>(Arrays.asList(finding
.getText().toString().split(",")));
if (!finding.getText().toString().equalsIgnoreCase("")) {
if (!finding.getText().toString().substring(finding.getText().toString().length() - 1,
finding.getText().toString().length()).equalsIgnoreCase(",")) {
textfinding = ","; //check if last character is == ,
}
}
for (int i = 0; i < CustomDialog.service.getFindingsArray().length; i++) {
if (_selectionsFindings[i] //check which item is checked
&& !values.contains(CustomDialog.service.getFindingsArray()[i].trim())) { // check if edittext contains the text
textfinding += CustomDialog.service.getFindingsArray()[i] + ","; // concatinate string
}
}
finding.append(textfinding); // this where error happens finding is an edittext ex(textfinding = hard drive) finding should have "hard drive"
// but it is not displayed but when i use getText() i can get "hard drive"
}
答案 0 :(得分:3)
当我点击按钮时,我可以通过“你好”来获得祝酒词 edittext)但edittext为空。这是一个错误吗?
您正在使用
在editText中设置文本 edit.setText("hello");
尝试删除它。
所以你的代码应该:
EditText edit=(EditText) findViewById(R.id.edittext);
Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(context,edit.getText().toString(),Toast.LENGTH_SHORT).show();
//edit.setText("hello"); <====== comment this line and check
}
}
答案 1 :(得分:0)
EditText edit=(EditText) findViewById(R.id.edittext);
Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
edit.setText("hello");
Toast.makeText(getApplicationContext(),edit.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
现在应该工作.. cheerz
答案 2 :(得分:0)
package com.example.edittext;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
EditText edit;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText) findViewById(R.id.editText1);
btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(getBaseContext(),edit.getText().toString(),Toast.LENGTH_SHORT).show();
edit.setText("hello");
}
});
}
@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;
}
}
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="74dp"
android:layout_marginTop="150dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:layout_marginTop="61dp"
android:text="Button" />
</RelativeLayout>