无法在android中单击按钮时显示textview [英] Unable to display textview on button click in android

查看:36
本文介绍了无法在android中单击按钮时显示textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建这个textview,然后点击这个按钮后显示,但是没有显示textview.

I want to create this textview, and then display it after this button is clicked, but no textview is displayed.

如果可能的话,我不想使用 findViewById(),因为我想在每次按下按钮时创建一个新的文本视图.我试过先做一个线性布局,但我看到很多网站都说你不需要,我宁愿不要.任何意见将是有益的.谢谢.

I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.

EditText name=layout.findViewById(R.id.enterName);
        final String Name=name.getText().toString();
        Button create=layout.findViewById(R.id.create);
        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
                ProgrammaticallyTextView.setText(Name);
                ProgrammaticallyTextView.setTextSize(22);
                popup.dismiss();
            }
        });

没有错误消息,logcat 也没有说有什么问题.

There are no error messages and the logcat doesn't say that anything is wrong.

推荐答案

试试这个:

private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   lLayout = (LinearLayout) findViewById(R.id.linearLayout);
   lButton = (Button) findViewById(R.id.button); 
   lEditText = (EditText) findViewById(R.id.editText);
   lButton.setOnClickListener(onClick());
   TextView textView = new TextView(this);
   textView.setText("Text New");
}

private OnClickListener onClick() {
   return new OnClickListener() {
    @Override
    public void onClick(View v) {
        lLayout.addView(createTextView(lEditText.getText().toString()));
    }
};
}

private TextView createTextView(String text) {
   final LayoutParams loutParams = new 
   LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   final TextView textView = new TextView(this);
   textView.setLayoutParams(loutParams );
   textView.setText("Text is " + text);
   return textView;
}

这篇关于无法在android中单击按钮时显示textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆