自定义AlertDialog让用户输入信息,再根据用户信息加载程序的解决方案
考虑用循环等待在oncreate方法中添加while循环。。。。。不可以
利用wait()方法等待,在Button的OnClickListener中用notify()唤醒。。。。。不可以
将Dialog创建之后的语句放到onResume()或onStart()方法中,在程序中控制使目前的进程stop或者pause然后重新启动后执行。。。。。。。没有找到做法
利用handler,将Dialog创建之后的语句放到handleMessage方法中执行。。。。。。。接下来的语句不允许放到handler中
利用不可见的EditText,在handler或者OnClickListener中赋值。。。。。。。因为oncreate仅执行一遍,无法刷新
在OnClickListener方法中,将要取的变量赋值后,重新打开自身的Activity。。。。。。。。。。。可以!!!!!!
http://hi.baidu.com/zqxy/blog/item/1320f199e29fd51c6e068cdc.html
利用下面的语句
startActivity(getIntent());
import java.io.File;
import java.lang.reflect.Field;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
public class KakuFirstAppActivity extends Activity {
/** Called when the activity is first created. */
private Handler handler;
private String sdPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
// start dialog
// boolean wait = true;
if (sdPath == null || sdPath.equals("")) {
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.dialog_sd_dir_input, null);
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("dialog");
// TableLayout sdDirInputDialog = (TableLayout)getLayoutInflater().inflate(R.layout.dialog_sd_dir_input, null);
// builder.setView(sdDirInputDialog);
builder.setView(view);
// 确定按钮
TextView show = (TextView)findViewById(R.id.show);
show.setText(sdPath == null?"":sdPath);
builder.setNeutralButton(R.string.okButton, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Message msg = new Message();
AlertDialog ad = (AlertDialog) dialog;
EditText t = (EditText) ad.findViewById(R.id.sdDirInputEdit);
if (t.getText().toString().equals("0")) {
TextView show = (TextView)findViewById(R.id.show);
show.setText(t.getText().toString());
msg.what = 0;
handler.sendMessage(msg);
// success flag
// TextView showGone = (TextView)findViewById(R.id.showGone);
// showGone.setText("pass");
try {
//关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
} else {
t.setText("");
try {
//不关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
// 取消按钮
builder.setNegativeButton("cancle", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
try {
//关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
finish();
}
});
// handler
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
TextView show = (TextView)findViewById(R.id.show);
sdPath = show.getText().toString();
// handle接受到messege之后如何刷新页面
}
}
};
// builder.setPositiveButton(R.string.okButton, new DialogInterface.OnClickListener(){
// public void onClick(DialogInterface dialog, int which){
// AlertDialog ad = (AlertDialog) dialog;
// EditText t = (EditText) ad.findViewById(R.id.sdDirInputEdit);
// //Toast.makeText(this, t.getText().toString(), Toast.LENGTH_LONG).show();
// TextView show = (TextView)findViewById(R.id.show);
// show.setText(t.getText().toString());
//// // success flag
//// TextView showGone = (TextView)findViewById(R.id.showGone);
//// showGone.setText("pass");
// }
// });
builder.create().show();
}
// builder.setPositiveButton("yes", new DialogInterface.OnClickListener(){
// //@Override
// public void onClick(DialogInterface dialog, int which){
// if(which == Dialog.BUTTON_POSITIVE){
// AlertDialog ad = (AlertDialog) dialog;
// EditText t = (EditText) ad.findViewById(R.id.sdDirInputEdit);
// //Toast.makeText(this, t.getText().toString(), Toast.LENGTH_LONG).show();
// TextView show = (TextView)findViewById(R.id.show);
// show.setText(t.getText().toString());
// }
// }
// });
// while (wait) {
// TextView showGone = (TextView)findViewById(R.id.showGone);
// if (showGone.getText().toString().equals("pass")) {
// wait = false;
// }
// }
TextView show1 = (TextView)findViewById(R.id.show1);
show1.setText("it has changed when you are inputing dialog");
Toast.makeText(this, "This is Toast view", Toast.LENGTH_LONG).show();
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// 改变显示
final TextView show = (TextView)findViewById(R.id.show);
//show.setText("Hello Android" + new java.util.Date());
//show.setText(getPathSDCard());
show.setText(getCacheDir().toString() + getFilesDir().toString());
}
});
}
/**
* get the path of SDCard
* return: String
*/
public String getPathSDCard(){
String SDCardDir = "";
File SDCard = null;
// is SDCard exist
boolean isSdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED);
//if exist
if (isSdCardExist) {
SDCard = Environment.getExternalStorageDirectory();
SDCardDir = SDCard.toString();
} else {
SDCardDir = "SDCard is not exist";
}
return SDCardDir;
}
}
-------------------------------------------------------------------
// 如果路径不正确的话
if (!isSDCardPath(externalSDPath)) {
isgetSDCardPath = GET_SD_CARD_PATH_NO;
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.dialog_sd_path_input, null);
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("dialog");
builder.setView(view);
// 确定按钮
builder.setPositiveButton(R.string.Ensure, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
// Message msg = new Message();
AlertDialog ad = (AlertDialog) dialog;
EditText t = (EditText) ad.findViewById(R.id.DlgSdPathInptTxt);
String txtString = t.getText().toString();
// 如果路径正确的话
if (isSDCardPath(txtString)) {
if (txtString.endsWith("/")) {
externalSDPath = txtString.substring(0, txtString.length() - 1);
} else {
externalSDPath = txtString;
}
isgetSDCardPath = GET_SD_CARD_PATH_YES;
// msg.what = 0;
// dialogHandler.sendMessage(msg);
startActivity(getIntent());
// notify();
// startActivityForResult(new Intent(android.provider.Settings.ACTION_APPLICATION_SETTINGS), RESULT_OK);
// finish();
try {
//关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
} else {
t.setText("");
try {
//不关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
// 取消按钮
builder.setNegativeButton("cancle", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
try {
//关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
dialog.cancel();
MainActivity.this.finish();
}
});
builder.create().show();
// // handler
// dialogHandler = new Handler() {
// @Override
// public void handleMessage(Message msg) {
// switch (msg.what) {
// case 0:
// }
// }
//
// };
// try {
// wait();
// } catch (InterruptedException e) {
// //keep trying
// }
}
-------------------------------------------------
String sdCardPath = readPreferences("hcdInstaller", "sd_card_path");
if (sdCardPath != null) {
externalSDPath = sdCardPath;
}
// 如果路径不正确的话
if (!isSDCardPath(externalSDPath)) {
isgetSDCardPath = GET_SD_CARD_PATH_NO;
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.dialog_sd_path_input, null);
Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.title_dialog_input);
builder.setView(view);
// 确定按钮
builder.setPositiveButton(R.string.Ensure, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
// Message msg = new Message();
AlertDialog ad = (AlertDialog) dialog;
EditText t = (EditText) ad.findViewById(R.id.DlgSdPathInptTxt);
String txtString = t.getText().toString();
// 如果路径正确的话
if (isSDCardPath(txtString)) {
if (txtString.endsWith("/")) {
externalSDPath = txtString.substring(0, txtString.length() - 1);
} else {
externalSDPath = txtString;
}
isgetSDCardPath = GET_SD_CARD_PATH_YES;
// write the path of the SDCard into preference
writePreferences("hcdInstaller", "sd_card_path", externalSDPath);
// msg.what = 0;
// dialogHandler.sendMessage(msg);
startActivity(getIntent());
// notify();
// startActivityForResult(new Intent(android.provider.Settings.ACTION_APPLICATION_SETTINGS), RESULT_OK);
// finish();
try {
//关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
} else {
t.setText("");
try {
//不关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
// 取消按钮
builder.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
try {
//关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
dialog.cancel();
MainActivity.this.finish();
}
});
builder.create().show();
// // handler
// dialogHandler = new Handler() {
// @Override
// public void handleMessage(Message msg) {
// switch (msg.what) {
// case 0:
// }
// }
//
// };
// try {
// wait();
// } catch (InterruptedException e) {
// //keep trying
// }
}