首页 \ 问答 \ Android studio Getsharedpreferences(Android studio Getsharedpreferences)

Android studio Getsharedpreferences(Android studio Getsharedpreferences)

我在论坛上闲逛,看到很多用户正在寻找在文本字段中使用共享偏好的方法,并永久保存,而不必使用“呼叫”按钮。 我是thosse用户之一hehe ..现在我有一个用户名字段,我可以点击按钮“保存”以保存数据但是当我重新启动应用程序时,来自userfield(textfield)的数据消失了,我必须使用另一个按钮将数据“调用”回现场。 如何更改代码,以便在您单击保存刚刚写入的数据时,在重新启动应用程序时保留该数据。 我检索数据的代码如下所示。 “for123”是呼叫按钮的onclick。

}
//Hämta nummer knappen
    public void for123(View view){
        SharedPreferences sharedPref = getSharedPreferences("userInfo", Context.MODE_PRIVATE);

        String name = sharedPref.getString("username", "");
        numTxt.setText(name + " ");

}

这几乎是整个mainActivityjava文件,不知道如何导入java文件的开头呵呵..

Button sendSMS;
Button sendSMSaon;
Button sendSMSaoff;
Button sendSMSrela1;
Button sendSMSrela2;
EditText msgTxt;
EditText numTxt;
EditText aonTxt;
EditText aoffTxt;
EditText rela1txt;
EditText rela2txt;


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

    sendSMS = (Button) findViewById(R.id.skicka);
    sendSMSaon = (Button) findViewById(R.id.skickaaon);
    sendSMSaoff = (Button) findViewById(R.id.skickaaoff);
    sendSMSrela1 = (Button) findViewById(R.id.skickarela1);
    sendSMSrela2 = (Button) findViewById(R.id.skickarela2);

    msgTxt = (EditText) findViewById(R.id.Textmeddelande);
    numTxt = (EditText) findViewById(R.id.nummer);
   aonTxt = (EditText) findViewById(R.id.aon);
    aoffTxt = (EditText) findViewById(R.id.aoff);
    rela1txt = (EditText) findViewById(R.id.rela1txt);
    rela2txt = (EditText) findViewById(R.id.relä2txt);

    msgTxt.setVisibility(View.INVISIBLE);
    aonTxt.setVisibility(View.INVISIBLE);
    aoffTxt.setVisibility(View.INVISIBLE);
    rela1txt.setVisibility(View.INVISIBLE);
    rela2txt.setVisibility(View.INVISIBLE);

    sendSMSaoff.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           String mymsgaoff = aoffTxt.getText().toString();
                                           String theNumber = numTxt.getText().toString();
                                           sendMsg(theNumber, mymsgaoff);
                                       }

                                   }

    );

    sendSMSaon.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          String mymsgaon = aonTxt.getText().toString();
                                          String theNumber = numTxt.getText().toString();
                                          sendMsg(theNumber, mymsgaon);
                                      }

                                  }







    );
    sendSMS.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       String myMsg = msgTxt.getText().toString();
                                       String theNumber = numTxt.getText().toString();
                                       sendMsg(theNumber, myMsg);
                                   }

                               }
    );
    sendSMSrela1.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       String myMsgrela1 = rela1txt.getText().toString();
                                       String theNumber = numTxt.getText().toString();
                                       sendMsg(theNumber, myMsgrela1);
                                   }

                               }
    );
    sendSMSrela2.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          String mymsgrela2 = rela2txt.getText().toString();
                                          String theNumber = numTxt.getText().toString();
                                          sendMsg(theNumber, mymsgrela2);
                                      }

                                  }







    );


}

//Sparar numret

public void saveInfo(View view) {
    SharedPreferences sharedPref = getSharedPreferences("userInfo", Context.MODE_PRIVATE);


    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("username", numTxt.getText().toString());
    editor.apply();

    Toast.makeText(this, "saved", Toast.LENGTH_LONG).show();



}
//Hämta nummer knappen
    public void for123(View view){
        SharedPreferences sharedPref = getSharedPreferences("userInfo", Context.MODE_PRIVATE);

        String name = sharedPref.getString("username", "");
        numTxt.setText(name + " ");

}
private void sendMsg(String theNumber, String myMsg)
{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(theNumber, null, myMsg, null, null);



}

}


I have wandering around forums and seen plenty of users looking for a way to use sharedpreferences in a textfield and save it permanently and not having to use a "call" button. I am one of thosse users hehe.. Right now i have a username field where i can click on a button "save" to save the data but when i restart the app the data from the userfield (textfield) is gone and i have to use another button to "call" the data back to the field. How do i change the code so when you click save the data you just wrote in stays there when you restart the app. My code for retrieving the data looks like this. The "for123" is the onclick for the call button.

}
//Hämta nummer knappen
    public void for123(View view){
        SharedPreferences sharedPref = getSharedPreferences("userInfo", Context.MODE_PRIVATE);

        String name = sharedPref.getString("username", "");
        numTxt.setText(name + " ");

}

this is almost the whole mainActivityjava file, didn't know how to import the start of the java file hehe..

Button sendSMS;
Button sendSMSaon;
Button sendSMSaoff;
Button sendSMSrela1;
Button sendSMSrela2;
EditText msgTxt;
EditText numTxt;
EditText aonTxt;
EditText aoffTxt;
EditText rela1txt;
EditText rela2txt;


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

    sendSMS = (Button) findViewById(R.id.skicka);
    sendSMSaon = (Button) findViewById(R.id.skickaaon);
    sendSMSaoff = (Button) findViewById(R.id.skickaaoff);
    sendSMSrela1 = (Button) findViewById(R.id.skickarela1);
    sendSMSrela2 = (Button) findViewById(R.id.skickarela2);

    msgTxt = (EditText) findViewById(R.id.Textmeddelande);
    numTxt = (EditText) findViewById(R.id.nummer);
   aonTxt = (EditText) findViewById(R.id.aon);
    aoffTxt = (EditText) findViewById(R.id.aoff);
    rela1txt = (EditText) findViewById(R.id.rela1txt);
    rela2txt = (EditText) findViewById(R.id.relä2txt);

    msgTxt.setVisibility(View.INVISIBLE);
    aonTxt.setVisibility(View.INVISIBLE);
    aoffTxt.setVisibility(View.INVISIBLE);
    rela1txt.setVisibility(View.INVISIBLE);
    rela2txt.setVisibility(View.INVISIBLE);

    sendSMSaoff.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           String mymsgaoff = aoffTxt.getText().toString();
                                           String theNumber = numTxt.getText().toString();
                                           sendMsg(theNumber, mymsgaoff);
                                       }

                                   }

    );

    sendSMSaon.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          String mymsgaon = aonTxt.getText().toString();
                                          String theNumber = numTxt.getText().toString();
                                          sendMsg(theNumber, mymsgaon);
                                      }

                                  }







    );
    sendSMS.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       String myMsg = msgTxt.getText().toString();
                                       String theNumber = numTxt.getText().toString();
                                       sendMsg(theNumber, myMsg);
                                   }

                               }
    );
    sendSMSrela1.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       String myMsgrela1 = rela1txt.getText().toString();
                                       String theNumber = numTxt.getText().toString();
                                       sendMsg(theNumber, myMsgrela1);
                                   }

                               }
    );
    sendSMSrela2.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          String mymsgrela2 = rela2txt.getText().toString();
                                          String theNumber = numTxt.getText().toString();
                                          sendMsg(theNumber, mymsgrela2);
                                      }

                                  }







    );


}

//Sparar numret

public void saveInfo(View view) {
    SharedPreferences sharedPref = getSharedPreferences("userInfo", Context.MODE_PRIVATE);


    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("username", numTxt.getText().toString());
    editor.apply();

    Toast.makeText(this, "saved", Toast.LENGTH_LONG).show();



}
//Hämta nummer knappen
    public void for123(View view){
        SharedPreferences sharedPref = getSharedPreferences("userInfo", Context.MODE_PRIVATE);

        String name = sharedPref.getString("username", "");
        numTxt.setText(name + " ");

}
private void sendMsg(String theNumber, String myMsg)
{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(theNumber, null, myMsg, null, null);



}

}


原文:https://stackoverflow.com/questions/39417009
更新时间:2022-04-06 19:04

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)