我有同樣的問題,因爲這 第i設置視圖值創建
alertlog之前
LayoutInflater充氣= Primera.this .getLayoutInflater();
view = inflater.inflate(R.layout.dialog,null);
empezar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(Primera.this);
builder.setTitle(getResources().getString(R.string.dialog_codigo));
builder.setView(view);
builder.setPositiveButton(getResources().getString(R.string.dialog_aceptar), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
codigo = (EditText) view.findViewById(R.id.codigo);
ContentValues values = new ContentValues();
String valor;
valor = codigo.getText().toString();
values.put(Database.CODIGO_NOMBRE, valor);
mDbHelper.getWritableDatabase().insert(Database.TABLA_CODIGO, null, values);
if(codigo.getText().toString() == null || codigo.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Codigo incorrecto" , Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Codigo correcto" , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Primera.this, Producto.class);
intent.putExtra("opcion",0);
intent.putExtra("primera",1);
startActivity(intent);
}
}
});
builder.setNegativeButton(getResources().getString(R.string.dialog_atras), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
dialog.cancel();
}
});
builder.create();
builder.show();
}
});
我設置了按鈕內的值視圖,並解決了問題。
其工作代碼:
empezar.setOnClickListener(新View.OnClickListener(){ 公共無效的onClick(視圖v){
final AlertDialog.Builder builder = new AlertDialog.Builder(Primera.this);
builder.setTitle(getResources().getString(R.string.dialog_codigo));
LayoutInflater inflater = Primera.this.getLayoutInflater();
view = inflater.inflate(R.layout.dialog,null);
builder.setView(view);
builder.setPositiveButton(getResources().getString(R.string.dialog_aceptar), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
codigo = (EditText) view.findViewById(R.id.codigo);
ContentValues values = new ContentValues();
String valor;
valor = codigo.getText().toString();
values.put(Database.CODIGO_NOMBRE, valor);
mDbHelper.getWritableDatabase().insert(Database.TABLA_CODIGO, null, values);
if(codigo.getText().toString() == null || codigo.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Codigo incorrecto" , Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Codigo correcto" , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Primera.this, Producto.class);
intent.putExtra("opcion",0);
intent.putExtra("primera",1);
startActivity(intent);
}
}
});
builder.setNegativeButton(getResources().getString(R.string.dialog_atras), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
dialog.cancel();
}
});
builder.create();
builder.show();
}
});
但我需要讓我的自定義佈局。 .. – Luca 2014-09-03 13:35:45
看看http://androidexample.com/Custom_Dialog_-_Android_Example/index.php?view=article_discription&aid=88&aaid=111 – 2014-09-03 13:37:30
這就像我做的...在那個鏈接中他使用了一個按鈕,在我的代碼中使用內置的對話框按鈕,我們做同樣的事情。 – Luca 2014-09-03 13:46:18