Android Promt User Input Dialog
By Fanxtea | 1 Januari 2015Dalam tutorial ini, saya akan menambah baik contoh amaran dialog. Untuk membuat ia dapat menerima input daripada user sama seperti PromptDialog. Lebih khusus, ini adalah satu contoh pengubahsuaian AlertDialog.
- Susun Atur Fail Dua layout diperlukan dalam tutorial ini.
- res/layout/activity_main.xml - Menu utama
- res/layout/newpage.xml - Prompt Dialog
- Kod Fail : MainActivity.java
- Demo Run aplikasi.
Fail : res/layout/activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/buttonPrompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paparkan Prompt Dialog"/>
<EditText
android:id="@+id/editTextResult"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
Fail : res/layout/newpage.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Your Message : "
android:textAppearance="?android:attr/textAppearanceLarge"/>
<EditText
android:id="@+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus/>
</EditText>
</LinearLayout>
packagecom.fanxtea.helloworld;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
publicclass MainActivity extends Activity {
finalContext context =this;
privateButton button;
private EditText result;
publicvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// components from main.xml
button=(Button) findViewById(R.id.buttonPrompt);
result=(EditText) findViewById(R.id.editTextResult);
// add button listener
button.setOnClickListener(new OnClickListener(){
@Override
publicvoid onClick(View arg0){
// get prompts.xml view
LayoutInflater li =
LayoutInflater.from(context);
View promptsView =
li.inflate(R.layout.newpage, null);
AlertDialog.Builder alertDialogBuilder =
newAlertDialog.Builder(context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput =(EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener(){
publicvoid onClick(DialogInterface dialog,int id){
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener(){
publicvoid onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog =alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
Klik butang "Paparkan Prompt Dialog".
Isikan mesej anda dan klik "Ok".
Mesej anda akan muncul di menu utama.