Android体验系列之--AlertDialog

来源:互联网 发布:怎么做淘宝客挣钱2016 编辑:程序博客网 时间:2024/06/07 02:08

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/alert" android:text="Raise a Alert"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/toast" android:text="Make a Toast"></Button>
</LinearLayout>
MessageDemo代码:

public class MessageDemo extends Activity implements View.OnClickListener {
   
 Button alert;
 Button toast;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        alert=(Button)findViewById(R.id.alert);
        alert.setOnClickListener(this);
        toast=(Button)findViewById(R.id.toast);
        toast.setOnClickListener(this);
    }
    public void onClick(View view){
     if(view==alert){
      new AlertDialog.Builder(this).setMessage("eek!").setTitle("MessageDemo").setNeutralButton("Close", new DialogInterface.OnClickListener(){
       public void onClick(DialogInterface dlg,int sumthin){
        
       }
      }).show();
     }
     else{
      Toast.makeText(this,"<click,click>",Toast.LENGTH_SHORT).show();
     }
    }
}

效果图:

原创粉丝点击