输入后如何使EditText不可见?

时间:2016-04-07 10:05:17

标签: android button android-studio android-edittext textview

我想知道如何使TextView和EditText按顺序运行,这样我就不需要一行用于TextView,另一行用于EditView。我觉得它会让我的应用程序更加干净。对此有任何建议。

到目前为止,我将EditText连接到一个按钮,然后输出文本。我只想将它放在一行中。这是我到目前为止的代码。

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);
}



}

这是我到目前为止的一张照片。谢谢。 :d

Image

2 个答案:

答案 0 :(得分:0)

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:

<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:

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:

Image Output

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.

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?

答案 1 :(得分:0)

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) {

        }
    });


}

}

相关问题