在另一个布局中获取 EditText 的值

Get value of a EditText in another layout

我有一个对话框出现,用户可以在其中自定义他们从每个操作中获得的点数。该对话框基于不是 main_activity 的布局。 main activity 是在 onCreate 方法中的 setContentView 中使用的布局。

当我尝试获取值时,我只得到空值,(因为 EditText 所在的布局未设置为内容视图....也许?)

如何解决这个问题,以便在 "settings" 布局中获取 edittext 的值并可以在 MainActivity 中使用该值?

private int teamOnePoints=0;       
private int teamTwoPoints=0;
private boolean teamOneField=true;

private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;

private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ToggleButton toggle = (ToggleButton) 
findViewById(R.id.field_team_toggle);            //activates toggle button
    toggle.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean 
isChecked) {
            if (isChecked) {                                                           
//changes between the teams
                teamOneField=false;
            } else {
                teamOneField=true;
            }
        }
    });

    FloatingActionButton fab = findViewById(R.id.points_edit_button);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openDialog();
        }
    });
}

public void openDialog() {
    AlertDialog.Builder builder = new 
AlertDialog.Builder(MainActivity.this);
    LayoutInflater inflater = this.getLayoutInflater();
    View v = inflater.inflate(R.layout.settings, null);

    builder.setView(v);
    builder.setTitle(getResources().getString(R.string.edit_rules));
    builder.setPositiveButton(getResources().getString(R.string.ok_button), 
new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        applySettings();
        }
    });

builder.setNegativeButton(getResources().getString(R.string.cancel_button), 
null);
    builder.show();
}

private void applySettings(){

    EditText newBurnedPoints = findViewById(R.id.burned_edit);
    burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());   

   EditText newLyrePoints = findViewById(R.id.lyre_edit);
    lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());

    EditText newOneHandLyrePoints = findViewById(R.id.one_hand_lyre_edit);
    oneHandLyrePoints=Integer.parseInt
                (newOneHandLyrePoints.getText().toString());

    EditText newHomeRunPoints = findViewById(R.id.home_run_edit);
    homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());

    EditText newRevPoints = findViewById(R.id.rev_edit);
    revPoints=Integer.parseInt(newRevPoints.getText().toString());

    EditText newTeamOneName = findViewById(R.id.name_one_edit);
    teamOneName= newTeamOneName.getText().toString();
    TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
    setNewTeamOneName.setText(teamOneName);

    EditText newTeamTwoName =findViewById(R.id.name_two_edit);
    teamTwoName= newTeamTwoName.getText().toString();
    TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
    setNewTeamTwoName.setText(teamTwoName);
}

设置布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:id="@+id/settings_layout"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/name_one_settings"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/name_team_one"
        style="@style/settings_headings"
        />
    <TextView
        android:id="@+id/name_two_settings"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/name_team_two"
        style="@style/settings_headings"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <EditText
        android:id="@+id/name_one_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/team_one"
        android:maxLength="20"
        android:singleLine="true"
        />
    <EditText
        android:id="@+id/name_two_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/team_two"
        android:maxLength="20"
        android:singleLine="true"
        />
</LinearLayout>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/burned_points"
    android:paddingTop="16dp"
    style="@style/settings_headings"/>

    <EditText
        android:id="@+id/burned_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="1"
        android:maxLength="2"
        android:inputType="number"
        android:singleLine="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lyre_points"
    android:paddingTop="16dp"
    style="@style/settings_headings"/>

    <EditText
        android:id="@+id/lyre_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="3"
        android:maxLength="2"
        android:inputType="number"
        android:singleLine="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/one_hand_lyre_points"
    android:paddingTop="16dp"
    style="@style/settings_headings"/>

    <EditText
        android:id="@+id/one_hand_lyre_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="5"
        android:maxLength="2"
        android:inputType="number"
        android:singleLine="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/home_run_points"
    android:paddingTop="16dp"
    style="@style/settings_headings"/>

    <EditText
        android:id="@+id/home_run_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="5"
        android:maxLength="2"
        android:inputType="number"
        android:singleLine="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/rev_points"
    android:paddingTop="16dp"
    style="@style/settings_headings"/>

    <EditText
        android:id="@+id/rev_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="1"
        android:maxLength="2"
        android:inputType="number"
        android:singleLine="true"/>

</LinearLayout>

主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>

<LinearLayout
    android:id="@+id/team_names"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/name_one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/team_one"
        style="@style/Headings"
        />
    <TextView
        android:id="@+id/name_two"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/team_two"
        style="@style/Headings"
        />
</LinearLayout>

<LinearLayout
    android:id="@+id/team_points"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/points_one"
        android:layout_width="0dp"
        android:layout_height="88dp"
        android:layout_weight="1"
        android:autoSizeTextType="uniform"
        android:text="0"
        style="@style/TeamPoints"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="16dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/field_team"/>

        <ToggleButton
            android:id="@+id/field_team_toggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="@string/team_one"
            android:textOn="@string/team_two"
            style="@style/buttons"/>
    </LinearLayout>
    <TextView
        android:id="@+id/points_two"
        android:layout_width="0dp"
        android:layout_height="88dp"
        android:layout_weight="1"
        android:autoSizeTextType="uniform"
        android:text="0"
        style="@style/TeamPoints"
        />

</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/events"
    android:paddingTop="16dp"
    style="@style/Headings"/>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    >

    <Button
        android:id="@+id/burned_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="burned"
        android:text="@string/burned"
        style="@style/buttons"/>
    <Button
        android:id="@+id/lyre_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="lyre"
        android:text="@string/lyre"
        style="@style/buttons"
        />
    <Button
        android:id="@+id/one_hand_lyre_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="oneHandLyre"
        android:text="@string/oneHandLyre"
        style="@style/buttons"
        />
    <Button
        android:id="@+id/homeRun_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="homeRun"
        android:text="@string/homeRun"
        style="@style/buttons"
        />
    <Button
        android:id="@+id/rev_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="rev"
        android:text="@string/rev"
        style="@style/buttons"
        />
</TableLayout>

<Button
    android:id="@+id/reset_button"
    android:layout_width="168dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_marginTop="16dp"
    android:onClick="reset"
    android:text="@string/reset"
    style="@style/buttons"
    />
<android.support.design.widget.FloatingActionButton
    android:id="@+id/points_edit_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:src="@android:drawable/ic_menu_edit"
    android:layout_margin="16dp" />

</LinearLayout>

我强烈建议使用 SharedPreferences. This way, you can access the data even if you restart your application. If that does not work for you, you can use Intent

考虑阅读 this Whosebug post。

使用共享首选项:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.points), pointsPerActivity);
editor.commit();

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 1;
// or you can store the value somewhere
// getResources().getInteger(R.integer.defaultPointsPerActivity);
int pointsPerActivity = sharedPref.getInt(pointsPerActivity, defaultValue);

applySettings(v);

中传递视图对象
private int teamOnePoints=0;       
private int teamTwoPoints=0;
private boolean teamOneField=true;

private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;

private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ToggleButton toggle = (ToggleButton) 
findViewById(R.id.field_team_toggle);            //activates toggle button
    toggle.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean 
isChecked) {
            if (isChecked) {                                                           
//changes between the teams
                teamOneField=false;
            } else {
                teamOneField=true;
            }
        }
    });

    FloatingActionButton fab = findViewById(R.id.points_edit_button);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openDialog();
        }
    });
}

public void openDialog() {
    AlertDialog.Builder builder = new 
AlertDialog.Builder(MainActivity.this);
    LayoutInflater inflater = this.getLayoutInflater();
    View v = inflater.inflate(R.layout.settings, null);

    builder.setView(v);
    builder.setTitle(getResources().getString(R.string.edit_rules));
    builder.setPositiveButton(getResources().getString(R.string.ok_button), 
new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        applySettings(v);
        }
    });

builder.setNegativeButton(getResources().getString(R.string.cancel_button), 
null);
    builder.show();
}

     private void applySettings(View view){

            EditText newBurnedPoints = view.findViewById(R.id.burned_edit);
            burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());   

           EditText newLyrePoints = view.findViewById(R.id.lyre_edit);
            lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());

            EditText newOneHandLyrePoints = view.findViewById(R.id.one_hand_lyre_edit);
            oneHandLyrePoints=Integer.parseInt
                        (newOneHandLyrePoints.getText().toString());

            EditText newHomeRunPoints = view.findViewById(R.id.home_run_edit);
            homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());

            EditText newRevPoints = view.findViewById(R.id.rev_edit);
            revPoints=Integer.parseInt(newRevPoints.getText().toString());

            EditText newTeamOneName = view.findViewById(R.id.name_one_edit);
            teamOneName= newTeamOneName.getText().toString();
            TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
            setNewTeamOneName.setText(teamOneName);

            EditText newTeamTwoName =view.findViewById(R.id.name_two_edit);
            teamTwoName= newTeamTwoName.getText().toString();
            TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
            setNewTeamTwoName.setText(teamTwoName);
        }