在我的代码中,我有一个
我可以展示我的代码,但我认为这并没有什么用。
谢谢!
编辑:
感谢所有的回答。我刚刚进行了一次测试,它完美地工作了。
这是我的可行代码:
编辑2:但是为什么这段代码不起作用?
AlertDialog
和一个TextView
。我想在我的AlertDialog
中显示这个TextView
,但我不知道如何做。我不知道如何将View
添加到AlertDialog
中。我可以展示我的代码,但我认为这并没有什么用。
谢谢!
编辑:
感谢所有的回答。我刚刚进行了一次测试,它完美地工作了。
这是我的可行代码:
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();
}
}
编辑2:但是为什么这段代码不起作用?
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);
}
}
}