В настоящее время мое приложение состоит из EditText
и кнопки, расположенной в нижней части экрана. Если я набираю что-то в EditText
, а затем удаляю эту кнопку, то в верхней части экрана создается TextView
.Добавить текст прямо над клавиатурой
Но, что я хочу достичь, это то, что если я нажму эту кнопку, то создается TextView
прямо над полем EditText
. И если я сделаю это снова, я хочу, чтобы второй TextView
снова был создан прямо над EditText
, но ниже другого TextView
.
Это мой код.
Вот XML: (Это все внутри RelativeLayout
Но по какой-то причине я не могу положить, что здесь, потому что его не обнаружил код ....)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Senden"
android:layout_gravity="center_horizontal"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="470px"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
и моя активность:
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
final View.OnClickListener btnHandler = new View.OnClickListener() {
public void onClick(View v) {
String msg = editText.getText().toString();
editText.getText().clear();
linearLayout.addView(createNewTextView(msg));
View view = getCurrentFocus();
if(view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
};
button.setOnClickListener(btnHandler);
}
private TextView createNewTextView(String text) {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setTextColor(Color.BLACK);
textView.setText(text);
return textView;
}
Что нужно изменить, чтобы оно выглядело так, как будто я его хочу?
Когда вы говорите, что использование LinearLayout в качестве родительского вида, вы имеете в виду, что я должен заменить RelativLayout на LinearLayout и поместить edittext и кнопку внутри Это? – nummer92
Да, Edittext и Button будут принимать первые два индекса снизу, поэтому каждый раз, когда вы вставляете индекс набора текста, как 3 –
Так что я тоже должен сказать: linearLayout.addView (кнопка, 1) и linearLayout.addView (edittext, 1). Поскольку никакая моя кнопка не находится наверху, а edttext находится внизу – nummer92