简体   繁体   English

你如何在textview上打印android工作室代码中的字符串字符

[英]how do you print the characters of a string in android studio code on textview

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText editText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        editText = findViewById(R.id.editText);


    }
    public void display(View view)
    {
        String str=editText.getText().toString();
        char first=str.charAt(0);

        textView.setText(first);














  }

}

/*this code is to take input into a editText and then print characters of that string on the textView but i am not able to achieve that even thought the code doesn't show any errors on android studio code */ /*此代码是将输入输入到 editText,然后在 textView 上打印该字符串的字符,但我无法实现这一点,即使该代码在 android 工作室代码上没有显示任何错误 */

// whenever i press the button which is connected to the display function it just crashes the app*/ // 每当我按下连接到显示器 function 的按钮时,应用程序就会崩溃 */

setText() takes a CharSequence as parameter, not a char . setText()CharSequence作为参数,而不是char Use substring instead of chartAt .使用substring而不是chartAt

String str = editText.getText().toString();
String first = str.substring(0,1);
textView.setText(first);

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

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