Java 编辑警报对话框生成器中的文本
我在片段中有一个警报对话框:Java 编辑警报对话框生成器中的文本,java,android,android-fragments,android-alertdialog,Java,Android,Android Fragments,Android Alertdialog,我在片段中有一个警报对话框: AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), com.yanay.yanay.apps.odds.R.style.AppCompatAlertDialogStyle); LayoutInflater inflater = getActivity().getLayoutInflater(
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
com.yanay.yanay.apps.odds.R.style.AppCompatAlertDialogStyle);
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_numberpicker, null));
View view = inflater.inflate(R.layout.dialog_numberpicker, null);
final EditText editText = (EditText) view.findViewById(R.id.numberpicked);
if (editText.getText().toString() != null){
numberpicked = editText.getText().toString();
Log.d("Text Picked", numberpicked);}
Boolean reverse = true;
String title = "";
String dialogMessage = "";
String buttonTitle = "";
if (!didStart && replies == 0) {
buttonTitle = "Choose Odds";
dialogMessage = ping.getBody() + " Challenged You! Odds you: " + ping.getNum() + ". Choose out of:";
title = "New Challenge";
message = numberpicked;
}
builder.setTitle(title);
builder.setMessage(dialogMessage);
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
Log.d("DidStart", didStart.toString());
Log.d("Replies", replies.toString());
if (replies < 3) {
builder.setPositiveButton(buttonTitle,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Pinger pinger = mPingerAdapter.getItem(position);
// pingSomeone(pinger);
final Context context = getActivity();
EditText choosenNumber = (EditText) getView().findViewById(R.id.numberpicked);
Bundle data = new Bundle();
data.putString(PingerKeys.ACTION, GcmAction.PING_CLIENT);
String from = ping.getFrom();
data.putString(PingerKeys.TO, ping.getFrom());
//PingerKeys.NUM = "3333";
data.putString(PingerKeys.NUM, message);
Log.d("LOOK HERE ####", message);
data.putString(PingerKeys.SENDER,
mDefaultSharedPreferences.getString(RegistrationConstants.TOKEN, "Test"));
try {
GoogleCloudMessaging.getInstance(context)
.send(FriendlyPingUtil.getServerUrl(getActivity()),
String.valueOf("6969"), data);
AnalyticsHelper.send(context, TrackingEvent.PING_SENT);
} catch (IOException e) {
Log.w(TAG, "Could not ping client.", e);
}
}
});}
builder.show();
replies = replies + 1;
}
AlertDialog.Builder=新建AlertDialog.Builder(getActivity(),
com.yanay.yanay.apps.doffics.R.style.AppStyle);
LayoutFlater充气机=getActivity().GetLayoutFlater();
builder.setView(充气机.充气(R.layout.dialog_numberpicker,null));
视图=充气机。充气(R.layout.dialog\u numberpicker,null);
final EditText EditText=(EditText)view.findViewById(R.id.numberpicked);
if(editText.getText().toString()!=null){
numberpicked=editText.getText().toString();
Log.d(“文本已拾取”,数字已拾取);}
布尔反向=真;
字符串标题=”;
字符串dialogMessage=“”;
弦钮扣=”;
如果(!didStart&&replays==0){
buttonTitle=“选择赔率”;
dialogMessage=ping.getBody()+“向您发起挑战!您的几率:“+ping.getNum()+”。从中选择:”;
title=“新挑战”;
message=numberpicked;
}
建造商名称(名称);
builder.setMessage(dialogMessage);
builder.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
dialog.dismise();
}
});
Log.d(“DidStart”,DidStart.toString());
Log.d(“replays”,replays.toString());
如果(答复<3){
生成器。设置正按钮(按钮,
新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
//Pinger-Pinger=mPingerAdapter.getItem(位置);
//萍儿;
final Context=getActivity();
EditText ChooseNumber=(EditText)getView().findViewById(R.id.numberpicked);
Bundle data=新Bundle();
data.putString(PingerKeys.ACTION、GcmAction.PING_客户端);
字符串from=ping.getFrom();
data.putString(PingerKeys.TO,ping.getFrom());
//PingerKeys.NUM=“3333”;
data.putString(PingerKeys.NUM,message);
Log.d(“看这里”,消息);
data.putString(PingerKeys.SENDER,
mdefaultSharedReferences.getString(RegistrationConstants.TOKEN,“Test”);
试一试{
GoogleCloudMessaging.getInstance(上下文)
.send(FriendlyPingUtil.getServerUrl(getActivity()),
字符串。valueOf(“6969”),数据);
AnalyticsHelper.send(上下文,TrackingEvent.PING_SENT);
}捕获(IOE异常){
Log.w(标记“无法ping客户端”,e);
}
}
});}
builder.show();
回复=回复+1;
}
拾取的数字始终为空。如何获取输入到编辑文本中的文本值。对话框布局只是一个线性布局,内部有一个编辑文本 以下是从
对话框
LayoutInflater layoutInflater = LayoutInflater.from(YourActivity.this);
View rootView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivity.this);
alertDialogBuilder.setView(rootView);
final EditText yourEditText= (EditText) rootView .findViewById(R.id.edittext);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get the `EditText` value
Toast.makeText(YourActivity.this,"EditText value" + yourEditText.getText(),Toast.LENG_SHORT).show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
每次为布局充气时,它都是一个新的、不同的实例。您需要保留对正在
生成器上设置的视图的引用,并在该视图上调用findViewById()
,以获取您的编辑文本。