2017-09-22 66 views
0

這裏就是我需要從EditTextAlertDialog如何從AlertDialogBox(EditText.xml)字符串輸入

public void checkButton() { 
    leggspillere = (Button) findViewById(R.id.leggspillere);  
    final LayoutInflater inflater = this.getLayoutInflater();  
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(inflater.inflate(R.layout.create_user,null));    
    builder.setMessage("Lag en spiller"); 
    builder.setCancelable(true); 
    builder.setPositiveButton("Ok", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) {     
       // trying to get the EditText-string inside alertdialogbox input here. 
       final String Name = UserName.      
       Person person = new Person(Name, 0);      
       dialog.dismiss(); 
       spillere.setText(person.getName() + " " + person.getScore()); 
      } 
     } 
    ); 
    //sette andre knappen "cancel" 
    builder.setNegativeButton("no", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     } 
    ); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 

的XML得到了文本的方法是:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="64dp" 
     android:scaleType="center" 
     android:background="#FFFFBB33" 
     android:contentDescription="@string/app_name" /> 

    <EditText 
     android:id="@+id/UserName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="16dp" 
     android:inputType="textEmailAddress" /> 
</LinearLayout> 

嘗試從EditText.xml獲取輸入/字符串以創建person對象。 始終引用null對象。我找不到任何解決辦法,指向某處。

+0

Spillere是你想要設置文本的編輯文本嗎? – Journey

+0

這是一個TextView,我想直觀地表示所有添加的玩家。 – meister

+0

「這個問題」不應該從代碼開始,它應該從實際遇到的實際言語技術問題開始。 – JoxTraex

回答

0

你怎麼想訪問DialogInterface.OnClickListener內的editText?

嘗試在View對象中獲取充氣視圖並在該視圖對象變量上執行findViewById。此代碼下面應該很好地工作:

LayoutInflater inflater = this.getLayoutInflater(); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
final View dialogView = inflater.inflate(R.layout.dialog_alert, null); 
builder.setView(dialogView); 
builder.setMessage("Lag en spiller"); 
builder.setCancelable(true); 
builder.setPositiveButton("Ok", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       EditText editText = (EditText) dialogView.findViewById(R.id.d_alert_et); 
       Log.v("tag", "Edit text value: " + editText.getText()); 
      } 
     }); 
builder.setNegativeButton("no", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
AlertDialog alert = builder.create(); 
alert.show(); 

基本上得到在一個查看對象的放大的視圖:

最後查看dialogView = inflater.inflate(R.layout.dialog_alert,NULL);

然後做對即視圖對象findViewById:

的EditText EDITTEXT =(EditText上)dialogView.findViewById(R.id.d_alert_et);

editText.getText()

0

請注意。請參閱下面的代碼:https://www.mkyong.com/android/android-custom-dialog-example/

// custom dialog 
final Dialog dialog = new Dialog(context); 
dialog.setContentView(R.layout.custom); 
dialog.setTitle("Title..."); 

// set the custom dialog components - text, image and button 
TextView text = (TextView) dialog.findViewById(R.id.text); 
text.setText("Android custom dialog example!"); 
ImageView image = (ImageView) dialog.findViewById(R.id.image); 
**EditText edtName = (EditText) dialog.findViewById(R.id.edtName);** 
image.setImageResource(R.drawable.ic_launcher); 

Button dialogButton = (Button) 
dialog.findViewById(R.id.dialogButtonOK); 
// if button is clicked, close the custom dialog 
dialogButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     **String s = edtName.getText().toString();** 
     dialog.dismiss(); 
    } 
}); 

dialog.show(); 
} 
0

我更喜歡使用Material Dialog庫,因爲它是非常容易使用。

可以使用自定義視圖或從我的項目中使用simple input

1.simple輸入例如:

new MaterialDialog.Builder(this) 
    .title(R.string.input) 
    .content(R.string.input_content) 
    .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) 
    .input(R.string.input_hint, R.string.input_prefill, new MaterialDialog.InputCallback() { 
     @Override 
     public void onInput(MaterialDialog dialog, CharSequence input) { 
      // Do something 
     } 
    }).show(); 

2.custom查看示例:

 View view = LayoutInflater.from(context).inflate(R.layout.dialog_new_queue, null, false); 

     MaterialDialog dialog = new MaterialDialog.Builder(context) 
       .customView(view, false).build(); 
     AutoCompleteTextView tv = view.findViewById(R.id.dialog_add_q_tv); 
     Button btn = view.findViewById(R.id.dialog_add_queue_btn); 
     btn.setOnClickListener(v -> { 
      String input = tv.getText().toString().trim(); 
      Observable 
        .create(emitter -> { 
         if (input.isEmpty()) { 
          emitter.onError(new IllegalArgumentException("اسم صف خالی است")); 
         } else if (dao.getQueue(pref.getLastUsername(), input) != null) { 
          emitter.onError(new IllegalArgumentException("این اسم تکراری است!")); 
         } else { 
          QueueModel q = new QueueModel(input, pref.getLastUsername(), System.currentTimeMillis()); 
          dao.insert(q); 
          emitter.onComplete(); 
         } 
        }) 
        .subscribeOn(Schedulers.io()) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(o -> {},e ->{ 
         tv.setError(e.getMessage()); 
         tv.requestFocus(); 
        },() -> { 
         Toast.makeText(context, "صف جدید ساخته شد", Toast.LENGTH_SHORT).show(); 
         dialog.dismiss(); 
         loadData(); 
        }); 
     }); 
     dialog.show(); 
0

更換2行:

final String Name = UserName. 
Person person = new Person(Name, 0); 

通過

EditText editText = (EditText) dialogView.findViewById(R.id.UserName); 
Person person = new Person(editText.getText(), 0); 
+0

嗯,應用程序不會崩潰,但人物對象是(null,0)。仍似乎無法獲得EditText輸入。 – meister

+0

你正在輸入一個有效的電子郵件地址? – j3App

+0

不是純文本。輸入類型更改爲「文本」。 – meister

0

嘗試此

public void checkButton() { 
leggspillere = (Button) findViewById(R.id.leggspillere); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 

LayoutInflater inflater = this.getLayoutInflater(); 

builder.setView(inflater.inflate(R.layout.create_user, null)); 
builder.setMessage("Lag en spiller"); 
builder.setCancelable(true); 
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     //Change here 
     Dialog myDialog = (Dialog) dialog; 
     EditText editText = (EditText) myDialog.findViewById(R.id.UserName); 
     Person person = new Person(editText.getText(), 0); 
    } 
}) 
//sette andre knappen "cancel" 
builder.setNegativeButton("no", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     dialog.dismiss(); 
    } 
});  
builder.create().show(); 
}