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

Posted

技术标签:

【中文标题】在另一个布局中获取 EditText 的值【英文标题】:Get value of a EditText in another layout 【发布时间】:2019-01-28 11:57:31 【问题描述】:

我有一个对话框出现,用户可以在其中计算他们从每个动作中获得的分数。该对话框基于不是 main_activity 的布局。主要活动是 onCreate 方法中 setContentView 中使用的布局。

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

我该如何解决这个问题,以便我在“设置”布局中获取编辑文本的值,并可以在 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_
android:layout_
android:padding="24dp"
android:id="@+id/settings_layout"
>
<LinearLayout
    android:layout_
    android:layout_
    >

    <TextView
        android:id="@+id/name_one_settings"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:text="@string/name_team_one"
        style="@style/settings_headings"
        />
    <TextView
        android:id="@+id/name_two_settings"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:text="@string/name_team_two"
        style="@style/settings_headings"
        />
</LinearLayout>

<LinearLayout
    android:layout_
    android:layout_
    >

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


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

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

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

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

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

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

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

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

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

    <EditText
        android:id="@+id/rev_edit"
        android:layout_
        android:layout_
        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_
android:layout_
android:orientation="vertical"
tools:context=".MainActivity"
>

<LinearLayout
    android:id="@+id/team_names"
    android:layout_
    android:layout_
    >

    <TextView
        android:id="@+id/name_one"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:text="@string/team_one"
        style="@style/Headings"
        />
    <TextView
        android:id="@+id/name_two"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:text="@string/team_two"
        style="@style/Headings"
        />
</LinearLayout>

<LinearLayout
    android:id="@+id/team_points"
    android:layout_
    android:layout_
    android:orientation="horizontal">

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

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:paddingTop="16dp"
        >

        <TextView
            android:layout_
            android:layout_
            android:text="@string/field_team"/>

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

</LinearLayout>

<TextView
    android:layout_
    android:layout_
    android:gravity="center_horizontal"
    android:text="@string/events"
    android:paddingTop="16dp"
    style="@style/Headings"/>

<TableLayout
    android:layout_
    android:layout_
    android:paddingTop="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    >

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

<Button
    android:id="@+id/reset_button"
    android:layout_
    android:layout_
    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_
    android:layout_
    android:layout_gravity="end|bottom"
    android:src="@android:drawable/ic_menu_edit"
    android:layout_margin="16dp" />

</LinearLayout>

【问题讨论】:

如果您尝试存储设置或首选项,请查看SharedPreferences,它是此类值的存储选项。作为替代方案,可以通过将Bundle 或单个值放入启动另一个ActivityIntent 中,将值从一个Activity 传递到另一个@。 How do I pass data between Activities in Android application?的可能重复 【参考方案1】:

我强烈推荐使用SharedPreferences。这样,即使您重新启动应用程序,您也可以访问数据。如果这对您不起作用,您可以使用Intent

考虑阅读this *** 的帖子。

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

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于在另一个布局中获取 EditText 的值的主要内容,如果未能解决你的问题,请参考以下文章

如何从 alertDialog.setView(R.layout.some_layout) 获取 EditText 的值

在另一个活动的列表视图的编辑文本中获取另一个活动的价值

Android 中通过Java代码动态生成EditText视图,并循环遍历EditText取出遍历的ID和文本框的值

Android 设置EditText的光标样式

如何获取android安卓控件EditText中的内容

从子 Activity 中获取 EditText 的值