我想要做的是:當有人點擊它會打開一個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,但沒有工作,所以我決定刪除這個部分,並與一個教程,我發現從頭開始重寫。現在它正在工作,但我仍然不知道我做錯了什麼。
謝謝!
對於崩潰,請發佈LogCat輸出。 – Eric 2012-08-09 00:56:33
對於問題2,你可以有像tInf.setTag(「textview1」);你可以識別它textView.findViewWithTag(「textview1」);希望你明白這個主意。 – 2012-08-09 01:00:28
我用logcat和佈局編輯了這篇文章。 – 2012-08-09 02:03:56