android - 在自定义对话框中设置 TextView

标签 android dialog

我已经创建了一个自定义对话框,但我无法将文本设置到 java 对话框布局中的 TextView 中,我的程序崩溃了。我的错误是什么?

public class Total_CBC extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.total_cbc);
    final TextView RBC_T = (TextView) findViewById(R.id.total_cbc_text_rbc);

    Button RBC_B = (Button) findViewById(R.id.total_cbc_btn_rbc);

    //

    RBC_B.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            showCustomDialog(RBC_T);
        }
    });
}


protected void showCustomDialog(final TextView _RBC_T) {
    final Dialog dialog = new Dialog(Total_CBC.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_edit);
    final EditText editText = (EditText) dialog.findViewById(R.id.dialog_edit_edittext);
    Button button = (Button) dialog.findViewById(R.id.dialog_edit_btn);
    TextView titel = (TextView) findViewById(R.id.dialog_edit_text_title);
    titel.setText("RBC");
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            _RBC_T.setText(editText.getText().toString());
            dialog.dismiss();
        }
    });
    dialog.show();
}

} 当我删除这行程序时,我需要设置文本

titel.setText("RBC");

最佳答案

改变

 TextView titel = (TextView) findViewById(R.id.dialog_edit_text_title);

 TextView titel = (TextView) dialog.findViewById(R.id.dialog_edit_text_title);

关于android - 在自定义对话框中设置 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350430/

相关文章:

android - onKeyDown() 或 onBackPressed()

java - 只有加密图像应保存在文件中并解密图像以供预览

Symfony PHP 中的 JavaScript 确认(..) 等效项

jQuery UI 对话框和 Ajax POST、JSON

jQuery 对话框第二次未打开

java - 将 ArrayList<CustomClass> 存储到 SharedPreferences

android - 如何在不使用数据库的情况下保存我的应用程序的配置??? (使用简单的文本文件)

android - 使用导航组件时,DialogFragment 不会关闭

android - 为什么对话框不能与线程并行工作?

android - Android Studio 如何为 native 代码构建不同的架构?