<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" > <Button android:id="@+id/btnShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="101dp" android:text="Click Here" /> <TextView android:id="@+id/txtResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnShow" android:layout_centerHorizontal="true" android:layout_marginTop="56dp" android:text="result" /> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout_dialog" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/txtTitleUsername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="7dp" android:text="Username : " /> <EditText android:id="@+id/txtUsername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtTitleUsername" android:layout_centerHorizontal="true" android:layout_marginTop="7dp" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/txtTitlePassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtUsername" android:layout_centerHorizontal="true" android:layout_marginTop="7dp" android:text="Password : " /> <EditText android:id="@+id/txtPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/txtUsername" android:layout_below="@+id/txtTitlePassword" android:layout_marginTop="7dp" android:ems="10" android:inputType="textPassword" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final AlertDialog.Builder popDialog = new AlertDialog.Builder(this); final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); // btnShow Button btnShow = (Button) findViewById(R.id.btnShow); btnShow.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { final View Viewlayout = inflater.inflate(R.layout.activity_dialog, (ViewGroup) findViewById(R.id.layout_dialog)); popDialog.setIcon(android.R.drawable.btn_star_big_on); popDialog.setTitle("Input User and Password "); popDialog.setView(Viewlayout); // Button OK popDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); // txtUsername and Password (Dialog) EditText user = (EditText) Viewlayout.findViewById(R.id.txtUsername); EditText pass = (EditText) Viewlayout.findViewById(R.id.txtPassword); // txtResult (Main Screen) TextView result = (TextView) findViewById(R.id.txtResult); result.setText(" Username : " + user.getText().toString() + " \n" + " Password : " + pass.getText().toString() + " "); } }) // Button Cancel .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.cancel(); } }); popDialog.create(); popDialog.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท