共享的首选项imageview更改

时间:2019-08-12 06:10:22

标签: java android-studio sharedpreferences

我创建了一个通过输入密码来更改图像的应用程序。我想在关闭应用程序后将其存储。所以我尝试使用共享首选项。但是我失败了...为什么下面的代码不能正常工作?

(PS 我上传了我的MainActivity代码。如果您需要其他活动或布局来回答我的问题,请说出我的意思

<html>

<head>
  <title>MainActivity.java</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style type="text/css">
    .s0 {
      color: #cc7832;
    }
    
    .s1 {
      color: #a9b7c6;
    }
    
    .s2 {
      color: #6a8759;
    }
    
    .s3 {
      color: #808080;
    }
    
    .ln {
      color: #606366;
      font-weight: normal;
      font-style: normal;
    }
  </style>
</head>

<body bgcolor="#2b2b2b">
  <table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060">
    <tr>
      <td>
        <center>
          <font face="Arial, Helvetica" color="#000000">
            MainActivity.java</font>
        </center>
      </td>
    </tr>
  </table>
  <pre><a name="l1"><span class="ln">1    </span></a><span class="s0">package </span><span class="s1">com.swd.passwordtest2</span><span class="s0">;</span>
<a name="l2"><span class="ln">2    </span></a>
<a name="l3"><span class="ln">3    </span></a>import <span class="s1">androidx.appcompat.app.AppCompatActivity</span><span class="s0">;</span>
<a name="l4"><span class="ln">4    </span></a>import <span class="s1">android.content.Context</span><span class="s0">;</span>
<a name="l5"><span class="ln">5    </span></a>import <span class="s1">android.content.SharedPreferences</span><span class="s0">;</span>
<a name="l6"><span class="ln">6    </span></a>import <span class="s1">android.os.Bundle</span><span class="s0">;</span>
<a name="l7"><span class="ln">7    </span></a>import <span class="s1">android.view.View</span><span class="s0">;</span>
<a name="l8"><span class="ln">8    </span></a>import <span class="s1">android.widget.EditText</span><span class="s0">;</span>
<a name="l9"><span class="ln">9    </span></a>import <span class="s1">android.widget.ImageView</span><span class="s0">;</span>
<a name="l10"><span class="ln">10   </span></a>
<a name="l11"><span class="ln">11   </span></a>public class <span class="s1">MainActivity </span><span class="s0">extends </span><span class="s1">AppCompatActivity {</span>
<a name="l12"><span class="ln">12   </span></a>
<a name="l13"><span class="ln">13   </span></a>    ImageView original<span class="s0">;</span>
<a name="l14"><span class="ln">14   </span></a>    <span class="s1">EditText Pincode</span><span class="s0">;</span>
<a name="l15"><span class="ln">15   </span></a>    <span class="s1">ImageView change2</span><span class="s0">;</span>
<a name="l16"><span class="ln">16   </span></a>
<a name="l17"><span class="ln">17   </span></a>
<a name="l18"><span class="ln">18   </span></a>    <span class="s1">@Override</span>
<a name="l19"><span class="ln">19   </span></a>    <span class="s0">protected void </span><span class="s1">onCreate(Bundle savedInstanceState) {</span>
<a name="l20"><span class="ln">20   </span></a>        <span class="s0">super</span><span class="s1">.onCreate(savedInstanceState)</span><span class="s0">;</span>
<a name="l21"><span class="ln">21   </span></a>        <span class="s1">setContentView(R.layout.activity_main)</span><span class="s0">;</span>
<a name="l22"><span class="ln">22   </span></a>
<a name="l23"><span class="ln">23   </span></a>        <span class="s1">original = (ImageView)findViewById(R.id.original)</span><span class="s0">;</span>
<a name="l24"><span class="ln">24   </span></a>        <span class="s1">Pincode = (EditText)findViewById(R.id.pincode)</span><span class="s0">;</span>
<a name="l25"><span class="ln">25   </span></a>        <span class="s1">change2 = (ImageView)findViewById(R.id.original2)</span><span class="s0">;</span>
<a name="l26"><span class="ln">26   </span></a>
<a name="l27"><span class="ln">27   </span></a>        <span class="s1">SharedPreferences preferences = getSharedPreferences(</span><span class="s2">&quot;save&quot;</span><span class="s0">,</span><span class="s1">MODE_PRIVATE)</span><span class="s0">;</span>
<a name="l28"><span class="ln">28   </span></a>        <span class="s1">String original = preferences.getString(</span><span class="s2">&quot;original&quot;</span><span class="s0">, null</span><span class="s1">)</span><span class="s0">;</span>
<a name="l29"><span class="ln">29   </span></a>    <span class="s1">}</span>
<a name="l30"><span class="ln">30   </span></a>
<a name="l31"><span class="ln">31   </span></a>
<a name="l32"><span class="ln">32   </span></a>    <span class="s0">public void </span><span class="s1">inputClick(View view) {</span>
<a name="l33"><span class="ln">33   </span></a>        <span class="s3">//입력 버튼을 누르면 실행</span>
<a name="l34"><span class="ln">34   </span></a>
<a name="l35"><span class="ln">35   </span></a>        <span class="s0">if </span><span class="s1">(Pincode.getText().toString().equals(</span><span class="s2">&quot;123456&quot;</span><span class="s1">)) {</span>
<a name="l36"><span class="ln">36   </span></a>            original.setImageResource(R.drawable.correct)<span class="s0">;</span>
<a name="l37"><span class="ln">37   </span></a>            <span class="s1">Pincode.setText(</span><span class="s2">&quot;&quot;</span><span class="s1">)</span><span class="s0">;</span>
<a name="l38"><span class="ln">38   </span></a>
<a name="l39"><span class="ln">39   </span></a>            <span class="s1">SharedPreferences preferences = getSharedPreferences(</span><span class="s2">&quot;save&quot;</span><span class="s0">, </span><span class="s1">Context.MODE_PRIVATE)</span><span class="s0">;</span>
<a name="l40"><span class="ln">40   </span></a>            <span class="s1">SharedPreferences.Editor editor = preferences.edit()</span><span class="s0">;</span>
<a name="l41"><span class="ln">41   </span></a>            <span class="s1">editor.putString(</span><span class="s2">&quot;original&quot;</span><span class="s0">,</span><span class="s1">original.getResources().toString())</span><span class="s0">;</span>
<a name="l42"><span class="ln">42   </span></a>            <span class="s1">editor.commit()</span><span class="s0">;</span>
<a name="l43"><span class="ln">43   </span></a>        <span class="s1">}</span>
<a name="l44"><span class="ln">44   </span></a>
<a name="l45"><span class="ln">45   </span></a>        <span class="s0">if </span><span class="s1">(Pincode.getText().toString().equals(</span><span class="s2">&quot;222222&quot;</span><span class="s1">)) {</span>
<a name="l46"><span class="ln">46   </span></a>            change2.setImageResource(R.drawable.correctt)<span class="s0">;</span>
<a name="l47"><span class="ln">47   </span></a>            <span class="s1">Pincode.setText(</span><span class="s2">&quot;&quot;</span><span class="s1">)</span><span class="s0">;</span>
<a name="l48"><span class="ln">48   </span></a>        <span class="s1">}</span>
<a name="l49"><span class="ln">49   </span></a>
<a name="l50"><span class="ln">50   </span></a>        <span class="s0">else </span><span class="s1">{</span>
<a name="l51"><span class="ln">51   </span></a>            Pincode.setText(<span class="s2">&quot;&quot;</span><span class="s1">)</span><span class="s0">;</span>
<a name="l52"><span class="ln">52   </span></a>        <span class="s1">}</span>
<a name="l53"><span class="ln">53   </span></a>    }
<a name="l54"><span class="ln">54   </span></a>
<a name="l55"><span class="ln">55   </span></a>}
<a name="l56"><span class="ln">56   </span></a>
<a name="l57"><span class="ln">57   </span></a>
<a name="l58"><span class="ln">58   </span></a></pre>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

您需要使用“活动”的onCreate中的此信息来更新视图。

您有一个代码,可让您单击图片资源,并且如果您在共享首选项中具有“原始”首选项,则将其存储在字符串original中。但是随后您需要在视图上使用setImageResource使其起作用。

因此解决方案是使用onCreate中的代码或需要更新值的任何地方:

original.setImageResource(*value taken from SharedPrefs*);

更新:

  1. 您正在保存图像资源,但是如果只想保存该资源,则可能要保存original.getImageResource()

  2. 要在下一个应用程序打开时将此图像恢复到imageView中,您需要使用preferences.getString()获取此资源。

  3. 然后,您需要在活动onCreate()方法中设置可绘制图像,以便在您之前使用original设置original.setImageResource()值并传递{{1} }值作为可绘制地址。

P.S。图片指针保存在int中,而不是int中,因此请正确保存:

String

得到它

SharedPreferences preferences = getSharedPreferences("save", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("original", R.drawable.correct);
editor.commit();