来自xml的PositiveButton和NegativeButton的alertDialog

时间:2013-09-17 23:22:13

标签: android android-alertdialog

我有警报对话框,并且PositiveButton和NegativeButton都是以编程方式选址的,我想从xml布局中检索它们,我尝试这样做,但我是Android开发的新手,

任何帮助将不胜感激,谢谢。

MainActivity:

 public class MainActivity extends Activity {
final Context context = this;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     setContentView(R.layout.main);  

    TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
    tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));

    TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
    tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
    tv1.setText(Html.fromHtml(getString(R.string.introclusion)));



button = (Button) findViewById(R.id.button1);

// add button listener
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        LayoutInflater li = LayoutInflater.from(arg0.getContext());
        final View dialog_layoutView = li.inflate(R.layout.dialog_layout, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(arg0.getContext());

        alertDialogBuilder.setView(dialog_layoutView);
        alertDialogBuilder

                .setCancelable(false)
                .setPositiveButton("OK",

                        new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {

                            EditText username = (EditText) dialog_layoutView.findViewById(R.id.txt_name);
                            EditText password = (EditText) dialog_layoutView.findViewById(R.id.password);

                                if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
                                    if(username.getText().toString().equals("test") && password.getText().toString().equals("test")) {
                                Intent intent = new Intent(MainActivity.this,Text.class);
                                startActivity(intent);
                                finish();}
                                    else{
                                        // get your custom_toast.xml layout
                                        LayoutInflater inflater = getLayoutInflater();

                                        View layout = inflater.inflate(R.layout.custom_toast,
                                                (ViewGroup) findViewById(R.id.custom_toast));

                                        // set a dummy image
                                        ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
                                        image.setImageResource(R.drawable.ic_launcher);

                                        // set a message
                                        TextView text = (TextView) layout.findViewById(R.id.text_toast);
                                        text.setText("Wrong username or password");

                                        // Toast...
                                        Toast toast = new Toast(getApplicationContext());
                                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                                        toast.setDuration(Toast.LENGTH_LONG);
                                        toast.setView(layout);
                                        toast.show();}}}    


                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

    }

});

 }

 }

dialog_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10sp" >

<EditText
    android:id="@+id/txt_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/dialog_uname"
    android:singleLine="true" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword" >
</EditText>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="@string/dialog_submit" />

    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_ok"
        android:text="@string/dialog_cancel" />
</RelativeLayout>

编辑:

  public class MainActivity extends Activity {
final Context context = this;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     setContentView(R.layout.main);  

    TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
    tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));

    TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
    tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
    tv1.setText(Html.fromHtml(getString(R.string.introclusion)));



button = (Button) findViewById(R.id.button1);

// add button listener
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        LayoutInflater li = LayoutInflater.from(arg0.getContext());
        final View dialog_layoutView = li.inflate(R.layout.dialog_layout, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(arg0.getContext());

        alertDialogBuilder.setView(dialog_layoutView);
        alertDialogBuilder

        .setCancelable(false);

                        /////////

                Button okBtn= (Button) dialog_layoutView.findViewById(R.id.btn_ok);
                okBtn.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        EditText username = (EditText) dialog_layoutView.findViewById(R.id.txt_name);
                        EditText password = (EditText) dialog_layoutView.findViewById(R.id.password);

                            if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
                                if(username.getText().toString().equals("test") && password.getText().toString().equals("test")) {
                            Intent intent = new Intent(MainActivity.this,Text.class);
                            startActivity(intent);
                            finish();}
                                else{
                                    // get your custom_toast.xml layout
                                    LayoutInflater inflater = getLayoutInflater();

                                    View layout = inflater.inflate(R.layout.custom_toast,
                                            (ViewGroup) findViewById(R.id.custom_toast));

                                    // set a dummy image
                                    ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
                                    image.setImageResource(R.drawable.ic_launcher);

                                    // set a message
                                    TextView text = (TextView) layout.findViewById(R.id.text_toast);
                                    text.setText("Wrong username or password");

                                    // Toast...
                                    Toast toast = new Toast(getApplicationContext());
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                                    toast.setDuration(Toast.LENGTH_LONG);
                                    toast.setView(layout);
                                    toast.show();}}}}
                        );
                Button cancelBtn = (Button) dialog_layoutView.findViewById(R.id.btn_cancel);    

                cancelBtn.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        dialog.cancel();
                    }
                });
                ///////



        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

    }

});
   }
  }

2 个答案:

答案 0 :(得分:1)

您可以使用dialog_layout检索Buttons,就像使用EditText

一样
Button okBtn= (Button) dialog_layout.findViewById(R.id.btn_ok);
Button cancelBtn = (Button) dialog_layout.findViewById(R.id.btn_cancel);

然后设置onClickListener s

okBtn.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // some code
    }
});
cancelBtn.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // some code
    }
});

答案 1 :(得分:0)

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

      <Button
          android:id="@+id/dialogButtonCancell"
          android:layout_width="75dp"
          android:layout_height="75dp">

      <Button
          android:id="@+id/dialogButtonOK"
          android:layout_width="80dp"
          android:layout_height="70dp"/>

</RelativeLayout>

Java代码:

// custom dialog
final Dialog dialog = new Dialog(context);
//name of xml - custom
dialog.setContentView(R.layout.custom);

// set the custom dialog components - text, image and button
Button dialogButtonOK = (Button) dialog.findViewById(R.id.dialogButtonOK);
Button dialogButtonCancell = (Button) dialog.findViewById(R.id.dialogButtonCancell);

  dialog.setTitle("Warning!!!!!");

// if button is clicked, call some method..
dialogButtonOk.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    someMethod();
    }
});

dialogButtonCancell.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {                            
   dialog.dismiss();                        
   }
});
dialog.show();