我正在尝试创建自定义警报对话框,其中两个字段是编辑文本,另一个是微调器。问题是我只能看到微调器,我无法看到编辑文本。编辑文本可用,但微调器隐藏了编辑文本的内容。所以我的问题是我如何设置编辑文本和Spinner的位置。
以下是我的代码。
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
RecordAudio.this);
alertDialog.setTitle("Would you Like to save your Recording");
alertDialog.setMessage("Enter Audio Name");
alertDialog.setIcon(R.drawable.save_icon);
final EditText editTextAudioName = new EditText(RecordAudio.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
editTextAudioName.setLayoutParams(lp);
alertDialog.setView(editTextAudioName);
final Spinner spinnerListData = new Spinner(RecordAudio.this);
RelativeLayout.LayoutParams lpSpinner = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
spinnerListData.setLayoutParams(lpSpinner);
alertDialog.setView(spinnerListData);
dataManipulatorClass = new DataManipulatorClass(this);
names2 = dataManipulatorClass.selectAll();
stg1 = new String[names2.size()];
int x = 0;
String stg;
for (String[] name : names2) {
stg = "Class Name : " + name[1];
stg1[x] = stg;
x++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
RecordAudio.this, android.R.layout.simple_spinner_item, stg1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerListData.setAdapter(adapter);
spinnerListData.setWillNotDraw(false);
spinnerListData.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Spinner Item selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
alertDialog.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Code.audioName = editTextAudioName.getText().toString()
.trim();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
if (Code.i == true) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
stopRecording();
enableButtons(false);
}
});
}
}, 41000);
} else {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
stopRecording();
enableButtons(false);
}
});
}
}, 21000);
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
myChronometer.setBase(SystemClock.elapsedRealtime());
myChronometer.start();
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
enableButtons(false);
}
});
alertDialog.show();
答案 0 :(得分:1)
setView方法: -
将自定义视图设置为Dialog的内容。如果提供 view是ListView的一个实例,将使用浅色背景。
所以你需要在某些布局中添加两个视图,例如LinearLayout,然后调用setView方法
答案 1 :(得分:1)
这是因为您要将视图设置为警告对话框两次。 首先定义一个Linearlayout,然后将视图(编辑文本和微调器)添加到其中,然后将Linearlayout设置为Alert Dialog。
LinearLayout parentLayout = new LinearLayout( RecordAudio.this);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final EditText editTextAudioName = new EditText(RecordAudio.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); editTextAudioName.setLayoutParams(lp); parentLayout.addView(editTextAudioName); final Spinner spinnerListData = new Spinner(RecordAudio.this); RelativeLayout.LayoutParams lpSpinner = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); spinnerListData.setLayoutParams(lpSpinner); parentLayout.addView(spinnerListData);