Dialog(一)——对话框(Dialog)基本使用

MainActivity如下:
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
    private Button mToastButton;
    private Button mAlertDialogButton;
    private Dialog mDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mToastButton=(Button) findViewById(R.id.toastButton);
    	mToastButton.setOnClickListener(new ButtonOnClickListenerImpl());
    	mAlertDialogButton=(Button) findViewById(R.id.alertDialogButton);
    	mAlertDialogButton.setOnClickListener(new ButtonOnClickListenerImpl());
    }

	private class ButtonOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.toastButton:
                Toast.makeText(MainActivity.this, getResources().getString(R.string.toast_info), Toast.LENGTH_LONG).show();
                break;
			case R.id.alertDialogButton:
                Builder builder=new Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle(getResources().getString(R.string.dialog_title));
                builder.setMessage(R.string.dialog_message);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						 Toast.makeText(MainActivity.this, getResources().getString(R.string.dialog_no), Toast.LENGTH_SHORT).show();
					}
				});
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						 Toast.makeText(MainActivity.this, getResources().getString(R.string.dialog_ok), Toast.LENGTH_SHORT).show();
					}
				});
                
                mDialog=builder.create();
                mDialog.show();
				break;
			default:
				break;
			}
		}

	}

}

main.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/toastButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/toast_tip" 
        android:layout_marginTop="100dip"
        android:textSize="20sp"
     />
    
    <Button
        android:id="@+id/alertDialogButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_tip" 
        android:layout_marginTop="100dip"
        android:textSize="20sp"
     />

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值