我有一个对话框,当我只使用微调器时效果非常好,但当我尝试从编辑文本中获取一个值时,返回一个空指针,用于在OK按钮中进行计算。
以下是我的审议意见
//the alert box
public boolean dialogValuessP1() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Arena.this);
alertDialogBuilder.setTitle("Connecting Options");
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = li.inflate(R.layout.option, null);
EditText connectionNumber = (EditText)findViewById(R.id.txtConnectedBy);
connectedV = Integer.parseInt(connectionNumber.getText().toString());
spinnercategory = (Spinner) dialogView.findViewById(R.id.viewSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ConnectBY = spinnercategory.getItemAtPosition(arg2).toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setPositiveButton("Connect",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (ConnectBY == "Single Score") {
scoreV = 1 * connectedV;
} else if (ConnectBY == "Double Score") {
scoreV = 2 * connectedV;
} else if (ConnectBY == "Tripple Score") {
scoreV = 3 * connectedV;
}
alert = true;
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getApplicationContext(),
"Please choose Connection Pattern",
Toast.LENGTH_LONG).show();
// Arena.this.finish(); used to close the game
alert = false;
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return alert;
}
// option.xml是使用的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:paddingLeft="10dip" >
<Spinner
android:id="@+id/viewSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@array/category" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connectwith" />
<EditText
android:id="@+id/txtConnectedBy"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
答案 0 :(得分:0)
由于edittext位于警告对话框内 尝试使用
EditText connectionNumber =(EditText) dialogView .findViewById(R.id.txtConnectedBy);
并重新使用值
String edittextValue=connectionNumber.getText().toString()
我认为您可以使用自定义对话框功能
来实现此功能final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
EdittText text = (EdittText ) dialog.findViewById(R.id.textDialog);
Spinner spinimage = (Spinner) dialog.findViewById(R.id.spinner);
//spinner details here
dialog.show();
Button ok= (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
String text = text.getText().toString();
//do something with the edittext value
}
});
请尝试分享您的观点
答案 1 :(得分:0)
感谢您的建议,我对代码进行了以下更改,并且能够在同一个alertdialog中同时使用edittext和微调器
public boolean dialogValuessP1() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Connecting Options");
LayoutInflater li = LayoutInflater.from(context);
final View dialogView = li.inflate(R.layout.option, null);
final EditText input = (EditText) dialogView
.findViewById(R.id.txtConnectedBy);
spinnercategory = (Spinner) dialogView.findViewById(R.id.viewSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
position = arg2 + 1;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setPositiveButton("Connect",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
edittextValue = input.getText().toString();
connectedV = Integer.parseInt(edittextValue);
scoreV = position * connectedV;
playNext();
alert = true;
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getBaseContext(),
"It is not your turn to Play",
Toast.LENGTH_LONG).show();
alert = false;
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return true;
}