Android 如何使用一个按钮将getText和setText设置为3个不同的EditText和TextView

Android 如何使用一个按钮将getText和setText设置为3个不同的EditText和TextView,android,android-studio,button,textview,android-edittext,Android,Android Studio,Button,Textview,Android Edittext,这很简单。我有一个按钮,有一个onClickListener,它从EditText中获取一个文本,隐藏它,并将它设置为TextView,它显示在同一位置。 现在,我想用同一个按钮再编辑两个不同的文本和文本视图。代码如下 XML与我的按钮: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"

这很简单。我有一个按钮,有一个onClickListener,它从EditText中获取一个文本,隐藏它,并将它设置为TextView,它显示在同一位置。 现在,我想用同一个按钮再编辑两个不同的文本和文本视图。代码如下

XML与我的按钮:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = ".profil"
    android:orientation = "vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:background="@color/pozadina"
        android:id="@+id/rltv">
        <ImageView
            android:id="@+id/slikaProfil"
            android:layout_width="130dp"
            android:layout_height="160dp"
            android:src="@drawable/profilna"
            android:layout_centerInParent="true"/>
        <TextView
            android:id="@+id/textProfil1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/userProfil"
            android:layout_below="@+id/slikaProfil"
            android:layout_centerInParent="true"
            android:textSize="20sp"
            android:textStyle="bold"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_below="@+id/rltv">

        <TextView
            android:id="@+id/imeProfil"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Account info"
            android:textSize="30sp"
            android:textStyle="bold"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="10dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ime i prezime"
            android:layout_marginLeft="70dp"
            android:drawableLeft="@drawable/profil"
            android:drawablePadding="5dp"
            android:id="@+id/imeProfil1"
            android:textSize="16sp"
            android:layout_below="@+id/imeProfil"
            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Upisite ime i prezime"
            android:layout_marginTop="-5dp"
            android:layout_marginLeft="70dp"
            android:id="@+id/imeProfil2"
            android:textSize="16sp"
            android:background="@null"
            android:layout_below="@+id/imeProfil1"
            />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Telefon"
            android:layout_marginLeft="70dp"
            android:drawableLeft="@drawable/telefon"
            android:drawablePadding="5dp"
            android:id="@+id/telefonProfil1"
            android:textSize="16sp"
            android:layout_below="@+id/imeProfil2"
            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Upisite broj telefona"
            android:layout_marginTop="-5dp"
            android:layout_marginLeft="70dp"
            android:id="@+id/telefonProfil2"
            android:textSize="16sp"
            android:background="@null"
            android:layout_below="@+id/telefonProfil1"
            />



        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="E-mail"
            android:layout_marginLeft="70dp"
            android:drawableLeft="@drawable/mail"
            android:drawablePadding="5dp"
            android:id="@+id/mailProfil1"
            android:textSize="16sp"
            android:layout_below="@+id/telefonProfil2"
            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Upisite e-mail"
            android:layout_marginTop="-5dp"
            android:layout_marginLeft="70dp"
            android:id="@+id/mailProfil2"
            android:textSize="16sp"
            android:background="@null"
            android:layout_below="@+id/mailProfil1"
            />



        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Adresa"
            android:layout_marginLeft="70dp"
            android:drawableLeft="@drawable/adresa"
            android:drawablePadding="5dp"
            android:id="@+id/adresaProfil1"
            android:textSize="16sp"
            android:layout_below="@+id/mailProfil2"
            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Upisite adresu"
            android:layout_marginTop="-5dp"
            android:layout_marginLeft="70dp"
            android:id="@+id/adresaProfil2"
            android:textSize="16sp"
            android:background="@null"
            android:layout_below="@+id/adresaProfil1"
            />

        <Button
            android:id="@+id/prihvati"
            android:layout_width="150dp"
            android:layout_height="70dp"
            android:layout_below="@+id/adresaProfil2"
            android:layout_marginLeft="70dp"
            android:text="@string/submit"
            android:onClick="zamijeni"/>
    </RelativeLayout>
</RelativeLayout>
如你所见,我有一个if函数,它在单击时获取文本,使编辑文本不可见,文本可见,最后将文本设置为textView


我应该多做几个if函数,还是应该以不同的方式来完成。

您应该实现相同的逻辑,从EditText访问文本并将其显示在TextView中,您可以创建一个函数,单击此按钮即可调用该函数

只需使用edittext即可更有效地实现这一点。。无需使用文本视图。。我在下面给出一个示例演示

    public class profil extends Fragment {
        private Button dugme4;
        //private TextView text1;
        private EditText text2;

        public profil(){

        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            return inflater.inflate(R.layout.activity_profil, container, false);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

           // text1 = view.findViewById(R.id.imeProfil1);
            text2 = view.findViewById(R.id.imeProfil2);
            dugme4 = view.findViewById(R.id.prihvati);

           // text2.setVisibility(View.INVISIBLE);
            dugme4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (text2.isEnabled()){
                    //make it not editable and non focusable
                    text2.setEnable(false);
                    text2.setCursorVisible(false);
                    //for clearing underline of editText so that it looks like textview
                    text2.setBackgroundResource(android.R.color.transparent);
}else{
    text2.setEnabled(true);
    text2.setCursonVisible(true);
 // set any color what you want as underline
    text2.setBackgroundResource(android.R.color.black);
}
                }
            });


            }
    }

试试看。希望它能通过减少额外的元素和复杂性来解决你的问题

你知道,从哪个edittext获取数据?我试过这个,它看起来比我写的要好得多。但我有一个问题,当我输入文本并按下按钮后,当我再次按下按钮时,它不会让我更改它。因此,我想我需要添加一个新的点击,使其再次改变。尝试新的一个,让我知道谢谢你!它解决了整个问题,看起来不错!我接受了答案,但我不能投票,因为我是一个新用户。
    public class profil extends Fragment {
        private Button dugme4;
        //private TextView text1;
        private EditText text2;

        public profil(){

        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            return inflater.inflate(R.layout.activity_profil, container, false);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

           // text1 = view.findViewById(R.id.imeProfil1);
            text2 = view.findViewById(R.id.imeProfil2);
            dugme4 = view.findViewById(R.id.prihvati);

           // text2.setVisibility(View.INVISIBLE);
            dugme4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (text2.isEnabled()){
                    //make it not editable and non focusable
                    text2.setEnable(false);
                    text2.setCursorVisible(false);
                    //for clearing underline of editText so that it looks like textview
                    text2.setBackgroundResource(android.R.color.transparent);
}else{
    text2.setEnabled(true);
    text2.setCursonVisible(true);
 // set any color what you want as underline
    text2.setBackgroundResource(android.R.color.black);
}
                }
            });


            }
    }