简体   繁体   中英

Get value of a EditText in another layout

I have an dialog that appears where the user can costumize how many points they get from each action. The dialog is based on a layout that is not the main_activity. the main activity is the layout that is used in setContentView in the onCreate method.

when I try to get the values I am only getting null, (because the layout where the EditText is is not set as the contentview.... maybe?)

How do I solve this problem so that I get the value of the edittext in the "settings" layout and can use that value in the 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);
}

SETTINGS LAYOUT:

<?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>

MAIN LAYOUT

<?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>

I would strongly recommend using 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 .

Consider reading this StackOverflow post.

Using SharedPreferences:

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

pass view object in 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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM