下午好,
我想打开一个输入对话框,并根据TextView' labUrl'的值设置' editTextDialogUserInput'的默认EditText值。有没有一种简单的方法来实现这一目标?
非常感谢你的帮助。
祝福, 劳伦
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 :(得分:0)
您可以使用android popup library并尝试以下代码。它削减了很多切割器
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();
}
}