21. Use of AlertDialog Common Dialog

Target Effect In this interface, there is a text input box and a button, enter content in the text input box, then click...
Target Effect
Page Layout
Customize Dialog Style
Main Function Event Response

Target Effect

In this interface, there is a text input box and a button, enter content in the text input box, then click the button to bring up a customized normal dialog box, in which the prompt information is the information entered in the text box. The target effect map is as follows:



Page Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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" android:background="@drawable/dog4" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="19dp" android:layout_marginLeft="10dp" android:text="Please enter the content of the prompt box!" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/write" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="10dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Show the contents of the dialog" /> </LinearLayout>

Customize Dialog Style

package com.example.alertdialogdemo; import org.w3c.dom.Text; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.webkit.WebView.FindListener; import android.widget.TextView; /* * Common Dialog Class * Style: A dialog box with a normal button */ @SuppressLint({ "ValidFragment", "NewApi" }) public class MyDialogFragment extends DialogFragment { String showDialog=null; public MyDialogFragment(String message) { showDialog=message; } @SuppressLint("NewApi") @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog=null; android.app.AlertDialog.Builder b=new AlertDialog.Builder(getActivity()); b.setIcon(R.drawable.ic_launcher); b.setTitle("I am a custom normal dialog"); b.setMessage(showDialog); //Set buttons and add action monitoring for buttons b.setPositiveButton("Determine", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { } }); dialog=b.create(); return dialog; } }

Main Function Event Response

package com.example.alertdialogdemo; import android.os.Build; import android.os.Bundle; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; @SuppressLint("NewApi") public class MainActivity extends Activity { EditText write=null; Button show=null; final int COMMON_DIALOG=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); write=(EditText) findViewById(R.id.write); show=(Button) findViewById(R.id.show); //Set up listeners for buttons show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { //Call the created method to display the contents of the write String message=write.getText().toString(); showDialog(message); } //Create a normal dialog box with the OK button @SuppressLint ("NewApi") private void showDialog(String message) { MyDialogFragment myDialog=new MyDialogFragment(message); myDialog.show(getFragmentManager(), "warning"); } }); } @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; } }

4 July 2020, 11:25 | Views: 7051

Add new comment

For adding a comment, please log in
or create account

0 comments