2014-05-08 37 views
0

在我的android應用程序中,我需要在用戶輸入用戶名時檢查用戶名的可用性。通過單擊按鈕輸入用戶名和密碼後,我知道該如何操作......但我想在文本視圖中顯示它。就像用戶輸入'george'一樣,如果george已經註冊了,那麼geroge需要在文本視圖中以紅色顯示,否則以綠色顯示。 蔭給我的代碼用於檢查上buttonclickAndroid json檢查用戶名可用性

private ProgressDialog pDialog; 
     JSONParser jsonParser = new JSONParser(); 

    private static String url_create_data = "http://example.com/app/create_data1.php"; 


// JSON Node names 
    private static final String TAG_SUCCESS = "success";  
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.signup_xm); 


        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 

         if (isNetworkAvailable(getBaseContext())) 
         { 

         String name=editTextUserName.getText().toString(); 
         String password=editTextPassword.getText().toString(); 
         String confirmPassword=editTextConfirmPassword.getText().toString(); 

         String phoneNo = editMobileNumber.getText().toString(); 
         //String sms = Integer.toString(number); 


         //Intent intent = new Intent(SignUpActivity.this, RegisterActivity.class); 

         //intent.putExtra("number", sms + ""); 
         //startActivity(intent);    

         //new CreateNewProduct().execute();       

         // check if any of the fields are vacant 
         if(name.equals("")||password.equals("")||confirmPassword.equals("") || phoneNo.equals("")) 
         { 
           Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show(); 
           return; 
         } 
         // check if both password matches 
         if(!password.equals(confirmPassword)) 
         { 
          Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show(); 
          return; 
         } 
         else 
         { 
          new CreateNewProduct().execute(); 


         } 


class CreateNewProduct extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(SignUpActivity.this); 
      pDialog.setMessage("Creating a new account.."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 

      String name = editTextUserName.getText().toString(); 
      String password = editTextPassword.getText().toString(); 
      String mobile = editMobileNumber.getText().toString();  

      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("name", name)); 
      params.add(new BasicNameValuePair("password", password)); 
      params.add(new BasicNameValuePair("mobile", mobile)); 

      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(url_create_data, 
        "POST", params); 

      // check log cat fro response 
      Log.d("Create Response", json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 

Intent i = new Intent(SignUpActivity.this, RegisterActivity.class);    
        i.putExtra("number", sms + ""); 
        startActivity(i);     
        //closing this screen 
        finish(); 
       } else { 
        // failed to create product 
        return "false"; 
       }  

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 

     protected void onPostExecute(String result) 

     {   // TODO Auto-generated method stub 
      super.onPostExecute(result); 
         if (result == "false"){ 

          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context); 

           // set title 
           alertDialogBuilder.setTitle("Username already Exists...!"); 

           // set dialog message 
           alertDialogBuilder 
            .setMessage("Select another Username. Click 'Ok' to continue.") 
            .setCancelable(false) 

            .setNegativeButton("OK",new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog,int id) { 
              // if this button is clicked, just close 
              // the dialog box and do nothing 
              dialog.cancel(); 
             } 
            }); 

            // create alert dialog 
            AlertDialog alertDialog = alertDialogBuilder.create(); 

            // show it 
            alertDialog.show(); } 



      // Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show(); 
         pDialog.dismiss(); 
     } 
    } 
+0

問題是什麼? –

+0

在這個程序中,用戶名被檢查是否存在。當用戶輸入用戶名到edittext。當他按下按鈕..但是,而不是我需要檢查用戶名是否存在當用戶在edittext中輸入文本..我認爲這是edittext更改列表或類似的東西。當用戶輸入詳細信息時,結果需要在同一頁的文本視圖中顯示。所以如果用戶輸入'george',首先他會輸入g以使textview顯示g,但是如果g用戶名可用,那麼它將以綠色顯示,否則顯示爲紅色。所以他可以知道哪個 – Jocheved

+0

用戶名在輸入名稱本身時可用 – Jocheved

回答

-1

的用戶名如果​​你想改變的TextView的顏色編程

使用text.setTextColor(Color.rgb(255,0,0));你可以設置rgb text.setTextColor(Color.parseColor(「#FF0000」));用於從十六進制值中分析顏色

2

這對移動設備而言並不常見。如果你想這樣做,那麼每次用戶輸入或刪除任何字符時都必須調用你的api。您可以使用TextWatcher進行此操作。按照這個link

注意:你不應該這樣做,因爲它會每次用戶改變任何東西時調用api。所以這會導致更多的設備電池使用以及更多的服務器負載。

如果用戶名不可用,那麼你可以使用setTextColor屬性的EditText紅色顯示。