研究其父类时候发现,可以设置这么一条属性,在AlertDialog.Builder.create()之后才能调用这两个方法
方法一:
setCanceledOnTouchOutside(false);调用这个方法时,按对话框以外的地方不起作用。按返回键还起作用
方法二:
setCanceleable(false);调用这个方法时,按对话框以外的地方不起作用。按返回键也不起作用
这两个方法都属于Dialog方法,可查阅源码
修改后的源码如下:
package com.android.settings;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.EditText;
import android.text.method.PasswordTransformationMethod;
public class TSActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog();
}
private void AlertDialog()
{
final EditText editText = new EditText(this);
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
new AlertDialog.Builder(this)
.setTitle("please input passwd")
.setCancelable(false)
.setIcon(android.R.drawable.ic_dialog_info)
.setView(editText)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
String Thing = editText.getText().toString();
if(Thing.compareTo("600518") == 0)
{
Intent intent=new Inten(TSActivity.this,MSettings.class);
startActivity(intent);
Log.d("yj.chen","success-----"+Thing);
}
finish();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
finish();
}
})
.show();
};