Ref : http://androidbiancheng.blogspot.tw/2011/05/alertdialogbuilder-edittext.html
package
com.AndroidEditDialog;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
public
class
AndroidEditDialog
extends
Activity {
Button btnEdit;
TextView textOut;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnEdit = (Button)findViewById(R.id.edit);
textOut = (TextView)findViewById(R.id.textout);
btnEdit.setOnClickListener(btnEditOnClickListener);
}
private
Button.OnClickListener btnEditOnClickListener
=
new
Button.OnClickListener(){
@Override
public
void
onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder editDialog =
new
AlertDialog.Builder(AndroidEditDialog.
this
);
editDialog.setTitle(
"--- Edit ---"
);
// edit text
final
EditText editText =
new
EditText(AndroidEditDialog.
this
);
editText.setText(textOut.getText());
editDialog.setView(editText);
editDialog.setPositiveButton(
"OK"
,
new
DialogInterface.OnClickListener() {
// do something when the button is clicked
public
void
onClick(DialogInterface arg0,
int
arg1) {
textOut.setText(editText.getText().toString());
}
});
editDialog.setNegativeButton(
"Cancel"
,
new
DialogInterface.OnClickListener() {
// do something when the button is clicked
public
void
onClick(DialogInterface arg0,
int
arg1) {
//...
}
});
editDialog.show();
}};
}
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello"
/>
<
Button
android:id
=
"@+id/edit"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Edit"
/>
<
TextView
android:id
=
"@+id/textout"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"You can change it"
/>
</
LinearLayout
>
沒有留言:
張貼留言