如何在android中检索用户输入?

时间:2016-01-07 21:32:40

标签: android

我有一个编辑文本,我想获得用户输入并将该输入保存为String。这是我的代码:

protected TextView soru;
protected EditText kcevap;
protected Button gonder;
protected String input;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    kcevap = (EditText) findViewById(R.id.kcevap);
    gonder = (Button) findViewById(R.id.gonder);
    gonder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            input = kcevap.getText().toString().trim();
        }
    });
    soru = (TextView) findViewById(R.id.soru);
}

我的xml:

 android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="text1"
    android:id="@+id/soru"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="64dp"
    android:textSize="60dp" />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/kcevap"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="137dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Gonder"
    android:id="@+id/gonder"
    android:layout_alignTop="@+id/kcevap"
    android:layout_alignParentEnd="true"
    android:textSize="30dp"
    android:clickable="true"
    android:textColor="#010000"
    android:background="#12bfda" />

编辑新解决方案。谢谢你的帮助!实际上它说的是

Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button

2 个答案:

答案 0 :(得分:3)

班级的变量:

private EditText myEditText;
private Button gonder;

OnCreate()中(假设您在Activity班级)添加:

myEditText = (EditText) findViewById(R.id.myedittext);
gonder = (Button) findViewById(R.id.gonder);

gonder.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String input = myEditText.getText().toString().trim();
        //you have your string, do as you want with it 
    }
});

其中myedittext是您布局中EditText的ID。

答案 1 :(得分:0)

如您所知,您在xml中提到textview是可点击的。 所以

EditText kcevap;
 Textview gonder;
 @Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     kcevap = (EditText) findViewById(R.id.kcevap);
     gonder = (Button) findViewById(R.id.gonder);
     gonder.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        input = kcevap.getText().toString().trim();
    }
});

}