2016-01-07 2 views
1

У меня есть текст редактирования, и я хочу получить пользовательский ввод и удерживать этот ввод как строку. Вот мой код:Как получить пользовательский ввод в android?

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" /> 

Edited для нового решения. Спасибо за вашу помощь! На самом деле он говорит, что

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

Что вы пробовали? Чтение значения edittext? С какими проблемами вы столкнулись? –

ответ

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 в раскладка.

+0

На самом деле это говорит о том, что «Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView не может быть добавлен в android.widget.Button« –

+0

@ EmreŞen Отредактируйте свой вопрос и добавьте свой код из класса и XML. Я помогу вам исправить это. –

+0

Подготовлено спасибо за помощь! –

0

Как вы знаете, вы указали в своем xml, что текстовое изображение можно щелкнуть. So

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

} 
+0

Он хочет 'Button' не' TextView' ... –

+0

Если он хочет сменить кнопку gonder textview to button simple in xml –

+0

Кстати, он упоминает в своем xml, что textview доступен для кликов. –

Смежные вопросы