2016-02-28 59 views
1

下午好,Android。基於另一個文本警告對話框中設置默認文本

我想開一個輸入對話框,設置「editTextDialogUserInput」默認的EditText值的基礎上的TextView「的價值labUrl」。有沒有簡單的方法來實現這一目標?

非常感謝您的幫助。

最良好的祝願, 洛朗

package laurent.ch.domoos; 

/** 
* Created by Laurent on 27.02.2016. 
*/ 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

private Button btnStartDomoos; 
private Button btnChangeUrl; 
private TextView labUrl; 
private EditText result; 

public void onCreate(Bundle savedInstanceState) { 
    final Context context = this; 

    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.main); 

    showToast(); 

    btnStartDomoos = (Button) findViewById(R.id.buttonUrl); 
    btnChangeUrl = (Button) findViewById(R.id.btnChangeUrl); 
    labUrl = (TextView) findViewById(R.id.labUrl) ; 
    result = (EditText) findViewById(R.id.editTextDialogUserInput); 

    btnStartDomoos.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Intent intent = new Intent(context, WebViewActivity.class); 
      intent.putExtra("url", labUrl.getText().toString()); 

      startActivity(intent); 
     } 

    }); 

    btnChangeUrl.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // get prompts.xml view 
      LayoutInflater li = LayoutInflater.from(context); 
      View promptsView = li.inflate(R.layout.prompts, null); 

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

      // set prompts.xml to alertdialog builder 
      alertDialogBuilder.setView(promptsView); 

      final EditText userInput = (EditText) promptsView 
        .findViewById(R.id.editTextDialogUserInput); 

      // set dialog message 
      alertDialogBuilder 
        .setCancelable(false) 
        .setPositiveButton("OK", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog,int id) { 
            // get user input and set it to result 
            // edit text 
            labUrl.setText(userInput.getText()); 

            InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
            imm.hideSoftInputFromWindow(userInput.getWindowToken(), 0); 
           } 
          }) 
        .setNegativeButton("Cancel", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog,int id) { 

            InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
            imm.hideSoftInputFromWindow(userInput.getWindowToken(), 0); 

            dialog.cancel(); 
           } 
          }); 

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

      // show it 
      alertDialog.show(); 
     } 
    }); 
} 

private void showToast(){ 
    Context context = getApplicationContext(); 
    CharSequence text = "Bienvenue dans Domoos "; 
    int duration = Toast.LENGTH_SHORT; 

    Toast toast = Toast.makeText(context, text, duration); 
    toast.show(); 
} 
} 

回答

0

您可以使用android popup library,並嘗試下面的代碼。它削減大量cuttler

package laurent.ch.domoos; 

/** 
* Created by Laurent on 27.02.2016. 
*/ 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

private Button btnStartDomoos; 
private Button btnChangeUrl; 
private TextView labUrl; 
private EditText result; 

public void onCreate(Bundle savedInstanceState) { 
    final Context context = this; 

    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.main); 

    showToast(); 

    btnStartDomoos = (Button) findViewById(R.id.buttonUrl); 
    btnChangeUrl = (Button) findViewById(R.id.btnChangeUrl); 
    labUrl = (TextView) findViewById(R.id.labUrl); 
    result = (EditText) findViewById(R.id.editTextDialogUserInput); 

    btnStartDomoos.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
    Intent intent = new Intent(context, WebViewActivity.class); 
    intent.putExtra("url", labUrl.getText().toString()); 

    startActivity(intent); 
    } 

    }); 

    btnChangeUrl.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
    Pop.on(MainActivity.this).with().layout(R.layout.prompts) 
    .when(new Pop.Yah() { 
     @Override 
     public void clicked(DialogInterface dialog, View view) { 
     imm.hideSoftInputFromWindow(userInput.getWindowToken(), 0); 
     dialog.dismiss(); 
     } 
    }) 
    .when(new Pop.Nah() { 
     @Override 
     public void clicked(DialogInterface dialog, View view) { 
     imm.hideSoftInputFromWindow(userInput.getWindowToken(), 0); 
     dialog.dismiss(); 
     } 
    }).show(new Pop.View() { 
     @Override 
     public void prepare(View view) { 
     EditText userInput = (EditText) view 
     .findViewById(R.id.editTextDialogUserInput); 
     userInput.setText(labUrl.getText()); 
     } 
    }); 

    } 
    }); 
} 

private void showToast() { 
    Context context = getApplicationContext(); 
    CharSequence text = "Bienvenue dans Domoos "; 
    int duration = Toast.LENGTH_SHORT; 

    Toast toast = Toast.makeText(context, text, duration); 
    toast.show(); 
} 
} 
相關問題