用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

请选择技术分类

 *如果你找不到更细的分类,可以选择上级分类
当前位置:云代码 - 技术问答 - Android

EditText的setText方法沒有反應

 悬赏:10|提问者:漫步云端|浏览:4973
举报|2013-10-30
package com.desiner.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.desiner.phone_com.R;
import com.desiner.user.User;

public class LoginActivity extends Activity {
	private Button btLogin;
	private Button btAdd;
	private CheckBox check;
	private EditText password;
	private SharedPreferences sp;
	private AutoCompleteTextView userName;
	private String userNameValue, passwordValue;
	private boolean isChecked = true;

	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_login);

		btLogin = (Button) findViewById(R.id.btLogin);
		btAdd = (Button) findViewById(R.id.btAdd);
		check = (CheckBox) findViewById(R.id.check);
		password = (EditText) findViewById(R.id.password);
		userName = (AutoCompleteTextView) findViewById(R.id.userName);
		sp = this.getSharedPreferences("passwordFile", Context.MODE_WORLD_READABLE);
		check.setChecked(true);// 默認記住密碼
//		userName.setThreshold(1);// 輸入一個字母就開始自動提示
//		password.setInputType(InputType.TYPE_CLASS_TEXT
//				| InputType.TYPE_TEXT_VARIATION_PASSWORD);
		// 隐藏密码为InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81
		// 显示密码为InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91

		btLogin.setOnClickListener(new buttonListener());
		btAdd.setOnClickListener(new buttonListener());
		/*
		 * 下拉輸入框變更事件
		 */
		userName.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				/*
				 * 將帳號和密碼存入passwordfile文件
				 */
				String[] allUserName = new String[sp.getAll().size()];//  sp.getAll().size()返回的是有多少个键值对
				allUserName = sp.getAll().keySet().toArray(new String[0]);
				//  sp.getAll()返回一张hash map  
				//  keySet()得到的是a set of the keys.  
				//  hash map是由key-value组成的  
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(
						LoginActivity.this,
						android.R.layout.simple_dropdown_item_1line,
						allUserName);
				userName.setAdapter(adapter);
				// 設置數據適配器
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			/*
			 * 帳號變更後自動輸入相對應的密碼
			 */
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(isChecked){
					password.setText(sp
							.getString(userName.getText().toString(), "").toString());
					// 自動輸入密碼
				}
			}
		});
	}

	class isChecked implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			setChecked(isChecked);
		}

	}

	class buttonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			userNameValue = userName.getText().toString();
			passwordValue = password.getText().toString();
			switch (v.getId()){
			/**
			 * 登錄按鈕事件
			 */
			case R.id.btLogin:
				Toast toast = Toast.makeText(getApplicationContext(), "Loading....", 0);
				toast.setGravity(Gravity.CENTER, 0, 0);
				//創建圖片視圖對象
				ImageView imageView = new ImageView(getApplicationContext());
				//設置圖片
				imageView.setImageResource(R.drawable.wait);
				//獲得toast的佈局
				LinearLayout toastView = (LinearLayout) toast.getView();
				//設置此佈局爲橫向的
				toastView.setOrientation(LinearLayout.HORIZONTAL);
				//將ImageView加入到此佈局中的第一個位置
				toastView.addView(imageView,0);
				toast.show();
				User user = new User();
				user.setUserName(userName.getText().toString());
				user.setPassword(password.getText().toString());
				//判斷帳號和密碼
				if(userNameValue.equals(sp.getString("userName", "").toString()) 
						&& passwordValue.equals(sp.getString("password", ""))){
					//判斷記住密碼多選框的狀態
					if(isChecked){
						sp.edit().putString(userNameValue, passwordValue).commit();
					}
					Intent intent = new Intent(LoginActivity.this,Funcall.class);
					startActivity(intent);
					Toast.makeText(LoginActivity.this, "登錄成功", 0).show();
				} else {
					Toast.makeText(LoginActivity.this,"帳號或密碼錯誤", 0).show();
				}
				break;
			/*
			 * 註冊按鈕點擊事件
			 */
			case R.id.btAdd:
				new AlertDialog.Builder(LoginActivity.this)
					.setTitle("用戶註冊")
					.setMessage("確認好你的註冊信息無誤了嗎")
					.setPositiveButton("提交", 
							new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									// TODO Auto-generated method stub
									User user = new User();
									user.setUserName(userName.getText().toString());
									user.setPassword(password.getText().toString());
									userNameValue = userName.getText().toString();
									passwordValue = password.getText().toString();
									
									//記住用戶名和密碼
									Editor editor = sp.edit();
									editor.putString("userName",userNameValue);
									editor.putString("password",passwordValue);
									editor.commit();
									
									Intent intent = new Intent(LoginActivity.this,Funcall.class);
									startActivity(intent);
									Toast.makeText(LoginActivity.this, "註冊成功", 0).show();
								}
							}).setNegativeButton("返回", null).show();
					break;
				default:
					break;
					
					
			}
		}
	}

}
其中
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(isChecked){
password.setText(sp
.getString(userName.getText().toString(), "").toString());
// 自動輸入密碼
}
沒有得到執行效果

最佳回答
//打印这个结果看看是不是空的 

sp.getString(userName.getText().toString(), "").toString()

回答者:小蜜锋

等级:高级设计师

积分:7088

(1) | (0) 回答时间:2013-10-31

所有回答
举报|(1)|(0)2013-10-31
//打印这个结果看看是不是空的 

sp.getString(userName.getText().toString(), "").toString()

举报|(0)|(0)2014-05-29
!!!!!!!!!!!!!!!!!!
举报|(0)|(0)2014-12-11
kan bu dong
举报|(0)|(0)2015-01-14
//打印这个结果看看是不是空的  sp.getString(userName.getText().toString(), "").toString()

举报|(0)|(0)2015-01-16
你试试看,Settext("msg") 试试看
举报|(0)|(0)2015-11-24
  sp.getString(userName.getText+“”, "").toString()
举报|(0)|(0)2017-04-18
??
举报|(0)|(0)2017-04-20
台,有PC端网站,及公众号-服务号,我们现在想在PC端及公众号里嵌入视频播放器,用于我们自己服务器里的视频,用户购买后播放,现在我们自己找了几个,都不是太好,要不PC端播放不了,就是IOS苹果手机播放不了,请帮忙,费用好谈
举报|(0)|(0)2022-09-27
而通过不是人格安抚

继续回答


相关提问