在我的代码中,我有一个AlertDialog
and a TextView
。我想显示这个TextView
in my AlertDialog
但我不知道该怎么做。我不知道如何添加View
in a AlertDialog
.
我可以展示我的代码,但我认为它没有用。
Thank's
EDIT:
感谢您的所有回答。我刚刚做了一个测试,效果非常好。
这是我的工作代码:
package com.example.testalertdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout layout;
AlertDialog ad;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
ad = new AlertDialog.Builder(this).create();
tv1 = new TextView(this);
setContentView(layout);
tv1.setText("Test");
ad.setView(tv1);
ad.show();
}
}
Edit2:但是为什么这段代码不起作用?
package com.example.testalertdialog;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
@SuppressLint("HandlerLeak")
public class MainActivity extends Activity implements OnClickListener{
LinearLayout layout;
AlertDialog ad;
TextView tv1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
tv1 = new TextView(this);
b1 = new Button(this);
b1.setOnClickListener(this);
layout.addView(b1);
ad = new AlertDialog.Builder(this).create();
setContentView(layout);
tv1.setText("Test");
}
@Override
public void onClick(View v) {
if (v == b1) {
ad.setMessage("Chargement");
ad.show();
ad.setView(tv1);
}
}
}
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Create TextView
final TextView input = new TextView (this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input.setText("hi");
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)