简体   繁体   English

如何在android中添加自定义数据到textview?

[英]How can I add custom data to textview in android?

I'm new to JAVA and Android. Previously, I was working on Javascript & Jquery.我是 JAVA 和 Android 的新手。之前,我正在研究 Javascript 和 Jquery。

As in HTML using Javascript (or Jquery) we can add custom data attribute to any element.与使用 Javascript(或 Jquery)的 HTML 一样,我们可以向任何元素添加自定义数据属性。 for example-例如-

$("#element").data("customdata", "customvalue");

And later I can get the value by doing-后来我可以通过做 -

var customvalue = $("#element").data("customdata");

Is there any method available in Android to achieve this type of thing? Android有什么方法可以实现这种事情吗?

Like, if I need to set multiple type of strings to a single TextView and later get them as needed.比如,如果我需要将多种类型的字符串设置为单个 TextView,然后根据需要获取它们。

Thank you all in advance.谢谢大家。

You can get your textview from XML and set text on it.您可以从 XML 获取您的 textview 并在其上设置文本。 Something like this像这样的东西

TextView textView = findViewById(R.id.textview_id);
textView.setText("Your awesome text");

// Later fetch the text details back
String textDetails = textView.getText().toString();

This is a basic code sample of how to add and retrieve text from the text-view.这是有关如何从文本视图添加和检索文本的基本代码示例。 Hope this help your case希望这对您的情况有所帮助

You can use the View's setTag() and getTag() for that purpose.为此,您可以使用视图的 setTag() 和 getTag()。

However, most of the time there are cleaner ways like using ViewModel architecture etc. but that is a different story.然而,大多数时候有更简洁的方法,比如使用 ViewModel 架构等,但这是另一回事。

I saw your link.我看到了你的链接。 I think you can use AlertDialog.我想你可以使用 AlertDialog。
this is my code.I think you would need it.这是我的代码。我想你会需要它。

public class MainActivity extends AppCompatActivity {
   EditText e1;
    TextView t4;
   Buttob btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t4=(TextView)findViewById(R.id.t4);
        t4.setOnClickListener(t4click);
        e1=(EditText)findViewById(R.id.e1);
       btn=(Button)findViewById(R.id.btn);
      btn.setOnClickListener(btnlogin);

    }

 private Button.OnClickListener btnlogin=new Button.OnClickListener(){
    //click button

        public void onClick(View v){
            String account=e1.getText().toString();//get edittext value
            String tmp="";
            tmp=t4.getText().toString()+account; //get textview value and add 
  edittext string
            t4.setText(tmp);//insert new value
            }
        }
    };
    private TextView.OnClickListener t4click=new TextView.OnClickListener(){
        @Override
    //if click textview it,would show dialog
   //if your dialog value is from edittext ,you can reuse it.
        public void onClick(View view) {
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("product")

                    .setMessage("item:pen"+"\nid=A001"+"\nprice:10 USD")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialoginterface, int i)
                        {
                        }
                    })
                    .show();
        }
    };

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM