【问题标题】:How do I create a button dynamically based on user input?如何根据用户输入动态创建按钮?
【发布时间】:2021-06-21 15:17:45
【问题描述】:

我正在尝试制作一个应用程序,其中用户输入文本并按下回车键,并且按钮动态放置在不同的活动上,其文本设置为用户输入的内容。我不知道该怎么做,我正在考虑将用户输入保存在 sharedPreference 中,但我不确定从那里去哪里。

package com.example.opisa;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddGoalsActivity extends AppCompatActivity {

    // Create object of SharedPreferences.
    SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
    //now get Editor
    SharedPreferences.Editor editor= sharedPref.edit();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_goals);
    }
    public void addGoalText(View view) {
        EditText name = (EditText) findViewById(R.id.editText1);
        TextView mText = (TextView) findViewById(R.id.textView);
        if (name.getText().toString().isEmpty()) {
            mText.setText("Didnt type anything");
        } else {
            mText.setText(name.getText().toString());
        }
    }

    //put your value
     editor.putString("goals", name);
    //commits your edits
     editor.commit();

}

在用户输入他想要的内容后,我希望 mText 成为按钮上的文本值,该按钮会动态出现在不同的活动上。

【问题讨论】:

    标签: android button dynamic android-edittext sharedpreferences


    【解决方案1】:

    在你的第一个活动中写

    public class AddGoalsActivity extends AppCompatActivity {
    
        // Create object of SharedPreferences.
        SharedPreferences setValue ;
        //now get Editor
        SharedPreferences.Editor valueEditor;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_goals);
            setValue  = getSharedPreferences("key",Context.MODE_PRIVATE);
        }
        public void addGoalText() {
            
            valueEditor = setValue.edit();
            EditText name = (EditText)  findViewById(R.id.editText1);
            TextView mText = (TextView) findViewById(R.id.textView);
            if (name.getText().toString().isEmpty()) {
                mText.setText("Didnt type anything");
                valueEditor.putString("key","Didn't type");
            } else {
                mText.setText(name.getText().toString());
                valueEditor.putString("key", String.valueOf(name.getText()));
            }
            valueEditor.apply();
        }
    }
    

    如果你在某处调用 addGoalText,如果不使用 onchangelistener

    在您有按钮的第二个活动中

    public class SecondActivity extends AppCompatActivity {
    
        SharedPreferences setValue ;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);// your layout
            setValue  = getSharedPreferences("key",Context.MODE_PRIVATE);
            Button your_button =findViewById(R.id.buttonId);//write your own ID
            String S = setValue.getString("key","defaultValue");
            your_button.setText(S);
        }
        
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 2015-02-11
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      相关资源
      最近更新 更多