简体   繁体   English

输入后如何使EditText不可见?

[英]How do I make EditText invisible after input?

I was wondering how you could make the TextView and EditText run in sequence so that I wouldn't need one row for the TextView and another for the EditView. 我想知道如何使TextView和EditText按顺序运行,这样我就不需要一行用于TextView,另一行用于EditView。 I just feel it would make the app i'm creating a lot more clean. 我觉得这会让我的应用程序变得更加干净。 Any advice on that. 对此有任何建议。

So far I'd connected the EditText to a button which then outputs text.I simply just want it in one row. 到目前为止,我已经将EditText连接到一个按钮,然后输出文本。我只想将它放在一行中。 This is the code I have so far. 这是我到目前为止的代码。

public class EventActivity extends AppCompatActivity {
private EditText enter_txt;
private TextView txt_title;

public void buttonOnClick(View v) {
    Button button = (Button) v;
    enter_txt = (EditText) findViewById(R.id.enter_txt);
    txt_title = (TextView) findViewById(R.id.txt_title);
    txt_title.setText(enter_txt.getText());

}

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



}

Here's a picture of what I've got so far. 这是我到目前为止所得到的图片。 Thanks. 谢谢。 :D :d

Image 图片

Even though it was very hard to understand you, finally I see what you are trying to achieve. 虽然很难理解你,但最终我看到了你想要实现的目标。 Here is how exactly you do, what you desire: 以下是您的具体表现,您想要的是什么:

your activity_event.xml file layout for example: 例如,您的activity_event.xml文件布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button android:id="@+id/button"
    android:text="Button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="button"/>

<EditText android:id="@+id/edittext"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"/>

<TextView android:id="@+id/textview"
    android:text="Text from EditText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:visibility="gone"/>

and your EventActivity.class: 和您的EventActivity.class:

public class EventActivity extends Activity {

Button button;
TextView textView;
EditText editText;

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

    button = (Button)findViewById(R.id.button);
    textView = (TextView)findViewById(R.id.textview);
    editText = (EditText)findViewById(R.id.edittext);

}

public void button(View view) {
    textView.setText(editText.getText().toString());
    editText.setVisibility(View.GONE);
    textView.setVisibility(View.VISIBLE);
}

The result is: 结果是:

图像输出

The thing is that you set your EditText visibility to GONE and at the same time the visibility of your TextView to VISIBLE when button is pressed. 问题是,当按下按钮时,您将EditText可见性设置为GONE,同时将TextView的可见性设置为VISIBLE。

more information about the Views visibilities you can find at Google API: setVisibility API or for example at already discussed thread: Stackoverflow: How can I remove a button or make it invisible in Android? 有关您可以在Google API上找到的视图可见性的更多信息: setVisibility API或者例如已经讨论过的线程: Stackoverflow:如何在Android中删除按钮或使其不可见?

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText etHide = (EditText) findViewById(R.id.etHide);
    final Button btnShow = (Button) findViewById(R.id.btnShow);
    btnShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            etHide.setVisibility(View.VISIBLE);
        }
    });
    etHide.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            etHide.setVisibility(View.INVISIBLE);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


}

} }

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

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