本文介绍了在警报对话框中显示textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我有一个 AlertDialog
和一个 TextView
.我想在 AlertDialog
中显示此 TextView
,但是我不知道该怎么做.我不知道如何在 AlertDialog
中添加 View
.
In my code, I have an AlertDialog
and a TextView
. I'd like display this TextView
in my AlertDialog
but I don't know how to do it. I don't know how to add the View
in a AlertDialog
.
我可以显示我的代码,但我认为这不会有用.
I could show my code but I don't think it would be usefull.
谢谢
感谢您的所有回答.我只是做了一个测试,就可以很好地工作.
Thank's for all your answers. I just did a test and it works perfectly.
这是我的工作代码:
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:但是为什么这段代码不起作用?
But why doesn't this code work ?
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();
这篇关于在警报对话框中显示textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!