2012-08-09 142 views
0

我想要做的是:當有人點擊它會打開一個EditText上的AlertDialog一個TextView,這個人寫的EditText上的東西,點擊數在「確定」,然後TextView的設置是什麼該人在EditText上寫道。的EditText內的AlertDialog

但是我有兩個problemas。

第一:它不工作。當我點擊「確定」時,我的應用程序崩潰,我不知道我做錯了什麼。

二:我也有不止一個TextView的,我不知道如何設置especific的TextView的文本,而不必爲每個TextView的新alertdialog。如何在對話框中識別該人是否點擊了TextView A或B?或者我應該創建另一個AlertDialog?

我的代碼:

public class Mec extends Activity implements OnClickListener { 
ImageView iv; 

Button save; 

TextView tSup, tInf; 

EditText txt = null; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.loud); 
    bts(); 
    Bundle extras = getIntent().getExtras(); 
    String vds = extras.getString("omec"); 
    if (vds.equals("Hello")) { 
     iv.setImageResource(R.drawable.world); 
    } 
    else { 
     return; 
    } 
} 
private void bts() { 
    iv = (ImageView) findViewById(R.id.ivID); 
    save = (Button) findViewById(R.id.btSave); 
    tSup = (TextView) findViewById(R.id.txtSuperior); 
    tInf = (TextView) findViewById(R.id.txtInferior); 
    txt = (EditText) findViewById(R.id.et1); 
} 
private void poptxt() { 
    AlertDialog InserirTXT = new AlertDialog.Builder(Mec.this).create(); 
    LayoutInflater infle = getLayoutInflater(); 
    View txtlayout = infle.inflate(R.layout.poptxt, null); 
    InserirTXT.setView(txtlayout); 
    InserirTXT.setButton("OK", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      String texto = (String) txt.getText().toString(); 
      tSup.setText(texto); 
     } 
    }); 
    InserirTXT.setCancelable(true); 
    InserirTXT.setCanceledOnTouchOutside(true); 
    InserirTXT.show(); 
} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.btSave: 
     //After 
     break; 

    case R.id.txtSuperior: 
     poptxt(); 
     break; 

    case R.id.txtInferior: 
     poptxt(); 
     break; 
    } 
} 
} 

感謝的任何幫助都!

編輯: 佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFFFFF" 
android:orientation="vertical" 
android:weightSum="100" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_weight="73.68" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/ivID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:src="@drawable/badluck" /> 

    <TextView 
     android:id="@+id/txtInferior" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/ivID" 
     android:layout_alignParentLeft="true" 
     android:clickable="true" 
     android:gravity="center" 
     android:onClick="onClick" 
     android:text="Bottom text" 
     android:textColor="#FFFFFF" 
     android:textSize="35dp" 
     style="@style/estilotxt" /> 

    <TextView 
     android:id="@+id/txtSuperior" 
     style="@style/estilotxt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignTop="@+id/ivID" 
     android:clickable="true" 
     android:editable="true" 
     android:gravity="center" 
     android:onClick="onClick" 
     android:text="Top text" 
     android:textColor="#FFFFFF" 
     android:textSize="35dp" /> 

</RelativeLayout> 

<Button 
    android:id="@+id/btSave" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="20" 
    android:text="Save" /> 

</LinearLayout> 

的logcat:

08-08 22:23:55.101: W/dalvikvm(322): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
08-08 22:23:55.121: E/AndroidRuntime(322): FATAL EXCEPTION: main 
08-08 22:23:55.121: E/AndroidRuntime(322): java.lang.NullPointerException 
08-08 22:23:55.121: E/AndroidRuntime(322): at vds.cmc.Mec$1.onClick(Mec.java:89) 
08-08 22:23:55.121: E/AndroidRuntime(322): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 
08-08 22:23:55.121: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-08 22:23:55.121: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123) 
08-08 22:23:55.121: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-08 22:23:55.121: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method) 
08-08 22:23:55.121: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:521) 
08-08 22:23:55.121: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-08 22:23:55.121: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-08 22:23:55.121: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method) 
08-08 22:23:56.950: I/Process(322): Sending signal. PID: 322 SIG: 9 

編輯:謝謝你們。

我試圖iniciating的AlertDialog裏面的TXT,但沒有工作,所以我決定刪除這個部分,並與一個教程,我發現從頭開始重寫。現在它正在工作,但我仍然不知道我做錯了什麼。

謝謝!

+0

對於崩潰,請發佈LogCat輸出。 – Eric 2012-08-09 00:56:33

+0

對於問題2,你可以有像tInf.setTag(「textview1」);你可以識別它textView.findViewWithTag(「textview1」);希望你明白這個主意。 – 2012-08-09 01:00:28

+0

我用logcat和佈局編輯了這篇文章。 – 2012-08-09 02:03:56

回答

0

您的佈局文件和你的logcat輸出會在這裏幫助,但即使沒有它,我可以看到一個潛在的問題:

String texto = (String) txt.getText().toString(); 
tSup.setText(texto); 

鑑於你的描述,我相信txt是你AlertDialog編輯字段,因此在創建AlertDialog之前調用的方法bts()中進行初始化,它將爲null。將其初始化移至AlertDialog創建後。

+0

我發佈了Layout和LogCat。 – 2012-08-09 02:05:15

+0

而且我也嘗試過移動bts(); AlertDialog之後,並沒有工作。 – 2012-08-09 02:06:07

+0

@VictorDomingosSantiago:只需移動'txt =(EditText)findViewById(R.id.et1);'line,而不是整個'bts()'調用,否則會遇到其他問題!看看你的LogCat,你可能還需要用'AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());'而不是'Mec.this'來初始化你的對話框...... – 2012-08-09 02:59:05

0
public class Mec extends Activity implements OnClickListener { 

    AlertDialog.Builder builder; 
    LinearLayout lila; 
    EditText et; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.loud); 

     builder = new AlertDialog.Builder(this); 
     lila = new LinearLayout(this); 
     et = new EditText(this); 

     lila.addView(et); 

     builder.setView(lila); 
     builder.show(); 
    } 

} 

這應該會給你一個很好的警告對話框,裏面有一個EditText! :) 現在,如果將builder.show()放置在另一個按鈕的onClickListener內部,則僅在單擊該按鈕時纔會顯示警報對話框。我希望這就是你要找的。

編輯:並設置textviews,你也只會說類似textfield.setText(et.getText()); - 你可能不得不把它作爲一個字符串,但這是基本的想法...

+0

我很新,在這個,我不認爲我瞭解代碼...我得到這個錯誤「令牌」生成器「的語法錯誤,VariableDeclarator預期後此令牌」AlertDialog.Builder ;「line。 – 2012-08-09 02:29:59

+0

對不起,我犯了一個愚蠢的錯誤..你當然需要設置一個變量名/標識符..我編輯了代碼..應該工作。 – corecase 2012-08-09 08:24:35

1

執行以下操作

創建一個XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/layout_root" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      > 
<ImageView android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginRight="10dp" 
      /> 
<TextView android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:textColor="#FFF" 
      /> 
</LinearLayout> 

然後創建一個對話框,設置佈局作爲其內容查看

Context mContext = getApplicationContext(); 
Dialog dialog = new Dialog(mContext); 

dialog.setContentView(R.layout.custom_dialog); 
dialog.setTitle("Custom Dialog"); 

TextView text = (TextView) dialog.findViewById(R.id.text); 
text.setText("Hello, this is a custom dialog!"); 
ImageView image = (ImageView) dialog.findViewById(R.id.image); 
image.setImageResource(R.drawable.android); 
0

使你的代碼是這樣

private void poptxt() { 
AlertDialog InserirTXT = new AlertDialog.Builder(Mec.this).create(); 
LayoutInflater infle = getLayoutInflater(); 
View txtlayout = infle.inflate(R.layout.poptxt, null); 
InserirTXT.setView(txtlayout); 
tSup = (TextView) InserirTXT.findViewById(R.id.txtSuperior); //THE PROBLEM 
txt = (EditText) InserirTXT.findViewById(R.id.et1); 
InserirTXT.setButton("OK", new DialogInterface.OnClickListener() { 

    public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     String texto = (String) txt.getText().toString(); 
     tSup.setText(texto); 
    } 
}); 
InserirTXT.setCancelable(true); 
InserirTXT.setCanceledOnTouchOutside(true); 
InserirTXT.show(); 
} 

你是我在Activity的背景下對tSup and txt進行氮化,其中您設置的佈局loud不具有R.id.txtSuperior and R.id.et1。這兩個視圖在你的佈局poptxt。我對麼?你在poptxt()裏充值poptxt。因此,使用對話框查看而不是使用Activity's視圖來初始化它們。

相關問題