EditText不会从字段返回值

时间:2014-11-01 11:51:26

标签: android android-edittext gettext

你好我从EditText字段获取值

有问题

我的代码是:

public class GameSetupActivity extends Activity {

DatePicker datepicker; 
TimePicker timepicker;
DatabaseManager dbm = new DatabaseManager(this);
public EditText enemy_name;
public EditText date;
public EditText time;
public EditText place;
public String enemy_name_s;
public String date_s;
public String time_s;
public String place_s;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_setup);
    enemy_name = (EditText) findViewById(R.id.enemy_name_activity_game_setup );
    date = (EditText) findViewById(R.id.date);
    time = (EditText) findViewById(R.id.time);
    place = (EditText) findViewById(R.id.place);
    this.setTitle(getResources().getString(R.string.preparing_game));

}

public void chose_players_button(View v)
{
    enemy_name_s=enemy_name.getText().toString();
    date_s=date.getText().toString();
    time_s = time.getText().toString();
    place_s=place.getText().toString();
    Intent intent = new Intent(GameSetupActivity.this,ChosePlayersForGameActivity.class);       
    startActivity(intent);
}

和布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent" 
android:fillViewport="true"
android:background="@drawable/boisko_610"


android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GameSetupActivity" 
 >

<EditText
    android:id="@+id/enemy_name_activity_game_setup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:hint="@string/enemy_team_name" />

<Button
    android:id="@+id/chose_players_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/enemy_name_activity_game_setup"
    android:layout_below="@+id/enemy_name_activity_game_setup"
    android:layout_marginTop="22dp"
    android:onClick="chose_players_button"
    android:text="@string/chose_players" />

但是当我每次在enemy_name_s为空时调试它,即使我在app中使用enemy_name.setText(“something”)字段是空的,我也不知道什么是错的,请帮忙。

问题不在于那个方法public void chosen_players_button(View v)不起作用,确实如此。它的行enemy_name_s = enemy_name.getText()。toString()不起作用

2 个答案:

答案 0 :(得分:0)

我尝试使用下面的少量编辑代码,运行正常,发送java和xml的下一部分,有一些问题。

public class MainActivity extends Activity {

DatePicker datepicker; 
TimePicker timepicker;
public EditText enemy_name;
public EditText date;
public EditText time;
public EditText place;
public String enemy_name_s;
public String date_s;
public String time_s;
public String place_s;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_setup);
enemy_name = (EditText) findViewById(R.id.enemy_name_activity_game_setup );
//date = (EditText) findViewById(R.id.date); it is not in your xml that you sent
//time = (EditText) findViewById(R.id.time);
//place = (EditText) findViewById(R.id.place);

}

public void chose_players_button(View v)
{
enemy_name_s=enemy_name.getText().toString();
//date_s=date.getText().toString();
//time_s = time.getText().toString();
//place_s=place.getText().toString();

Toast.makeText(this, enemy_name_s, Toast.LENGTH_SHORT).show();
}


}

和我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent" 
android:fillViewport="true"
android:gravity="top"
tools:context=".GameSetupActivity" 
 >

<EditText
android:id="@+id/enemy_name_activity_game_setup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>

<Button
android:id="@+id/chose_players_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/enemy_name_activity_game_setup"
android:layout_below="@+id/enemy_name_activity_game_setup"
android:layout_marginTop="22dp"
android:onClick="chose_players_button"/>

</RelativeLayout>

答案 1 :(得分:0)

它一定是项目文件的东西,因为我创建了一个新项目并复制了其他calsses,它改变了一个机器人,它工作正常。