java - 获取另一个布局中 EditText 的值

标签 java android dialog layout-inflater setcontentview

我有一个对话框,用户可以在其中自定义他们从每个操作中获得的分数。该对话框基于非 main_activity 的布局。主要 Activity 是在 onCreate 方法中的 setContentView 中使用的布局。

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

如何解决这个问题,以便在“设置”布局中获取 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 。这样,即使重新启动应用程序,您也可以访问数据。如果这对您不起作用,您可以使用 Intent

考虑阅读this StackOverflow 帖子。

使用共享首选项:

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

关于java - 获取另一个布局中 EditText 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963185/

相关文章:

android - 如何在 android 中的 facebook 上发布图像?

java - 对话框标题栏中的自定义按钮

user-interface - WIX 相当于 C# 文本框?

android - 显示自定义时间选择器期间应用强制关闭

java - 解析的日期有微小的差异

java - Mysql:使用java和hibernate的java.sql.Time数据类型返回java.sql.Date

android - 从 BroadcastReceiver 或服务类启动新 Activity

java - 读取正在写入的文件 - 锁定它?

java - 如何避免写入太多数据输出而阻塞线程?

Android sdk main.out.xml解析错误