Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Input Text ?" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Show AlertDialog" />
</RelativeLayout>
Example : MainActivity.java
package com.example.alertdialog1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.app.AlertDialog;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
final Button btn1 = (Button) findViewById(R.id.button1);
final TextView editT1 = (TextView)findViewById(R.id.editText1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog ad = adb.create();
String strTxt = editT1.getText().toString();
ad.setMessage("ข้อความของคุณคือ : " + strTxt);
ad.show();
}
});
}
}
ผลลัพธ์ :
ไม่มีความคิดเห็น:
แสดงความคิดเห็น