我在将文本设置为EditText时遇到了一些问题。没有错误发生,我可以从customDialog获取文本,但是当我将文本设置为edittext时,没有任何事情发生。
这是用于设置edittext的xml文件协议。
<LinearLayout
android:id="@+id/setting_layout_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
android:clickable="true">
<TextView
android:layout_height="wrap_content"
android:layout_weight="25"
android:clickable="false"
android:text="Firstname"
android:textStyle="bold"
android:layout_width="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="@+id/setting_textLayout_fname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="75"
android:clickable="false">
<android.support.design.widget.TextInputEditText
android:id="@+id/setting_edittext_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
这是处理我的alertdialog的代码。
package com.example.yggdrasil.realapplication;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class setting extends AppCompatActivity implements View.OnClickListener{
private android.support.v7.widget.Toolbar toolbar;
private LinearLayout linear_fname, linear_lname, linear_birthdate, linear_gender, linear_tel,
linear_carefname, linear_carelname, linear_careemail, linear_caretel, linear_range,
linear_min, linear_max;
private TextInputEditText input_fname;
private List<LinearLayout> layout_list;
private AlertDialog.Builder alertdialog;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
alertdialog = new AlertDialog.Builder(this);
layout_list = new ArrayList<LinearLayout>();
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.activity_setting_toolbar);
toolbar.setCollapsible(true);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Setting");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
input_fname = (TextInputEditText) findViewById(R.id.setting_edittext_fname);
input_fname.setText("test");
layout_list.add(linear_fname = (LinearLayout) findViewById(R.id.setting_layout_fname));
/*layout_list.add(linear_lname = (LinearLayout) findViewById(R.id.setting_layout_lname));
layout_list.add(linear_birthdate = (LinearLayout) findViewById(R.id.setting_layout_birthdate));
layout_list.add(linear_gender = (LinearLayout) findViewById(R.id.setting_layout_gender));
layout_list.add(linear_tel = (LinearLayout) findViewById(R.id.setting_layout_tel));
layout_list.add(linear_carefname = (LinearLayout) findViewById(R.id.setting_layout_caretaker_fname));
layout_list.add(linear_carelname = (LinearLayout) findViewById(R.id.setting_layout_caretaker_lname));
layout_list.add(linear_careemail = (LinearLayout) findViewById(R.id.setting_layout_caretaker_email));
layout_list.add(linear_caretel = (LinearLayout) findViewById(R.id.setting_layout_caretaker_tel));
layout_list.add(linear_range = (LinearLayout) findViewById(R.id.setting_layout_glucose_range));
layout_list.add(linear_min = (LinearLayout) findViewById(R.id.setting_textLayout_minglucose));
layout_list.add(linear_max = (LinearLayout) findViewById(R.id.setting_textLayout_maxglucose));*/
for(LinearLayout linearLayout : layout_list){
linearLayout.setOnClickListener(this);
}
}
public void onClick(View v){
if(v.getId() == R.id.setting_layout_fname){
final EditText edittext = new EditText(getApplicationContext());
alertdialog.setTitle("First Name");
alertdialog.setMessage("Enter your firstname: ");
alertdialog.setView(edittext);
alertdialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
final String fname = edittext.getText().toString();
Toast.makeText(setting.this, fname, Toast.LENGTH_SHORT).show();
setting.this.input_fname.setText( fname, TextView.BufferType.EDITABLE);
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alertdialog.create().show();
}
}
不知道我的代码会发生什么,Toast总是显示与我说的相同的值但是它没有显示在mainActivity的edittext中。
答案 0 :(得分:0)
代码有2个edittext,第1个是以编程方式创建的alertdialog中的editText,第2个是在xml文件中创建的editText。
我想要做的就是使用alertdialog中editText的值将setText设置为xml中的editText。我可以从alertdialog中的edittext获取值,但不能将setText设置为xml文件中的editText。
package com.example.yggdrasil.realapplication;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class setting extends AppCompatActivity implements View.OnClickListener{
private android.support.v7.widget.Toolbar toolbar;
private LinearLayout linear_fname, linear_lname, linear_birthdate, linear_gender, linear_tel,
linear_carefname, linear_carelname, linear_careemail, linear_caretel, linear_range,
linear_min, linear_max;
private TextInputEditText input_fname;
private List<LinearLayout> layout_list;
private AlertDialog.Builder alertdialog;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
alertdialog = new AlertDialog.Builder(this);
layout_list = new ArrayList<LinearLayout>();
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.activity_setting_toolbar);
toolbar.setCollapsible(true);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Setting");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
input_fname = (TextInputEditText) findViewById(R.id.setting_edittext_fname);
input_fname.setText("test");
layout_list.add(linear_fname = (LinearLayout) findViewById(R.id.setting_layout_fname));
for(LinearLayout linearLayout : layout_list){
linearLayout.setOnClickListener(this);
}
}
public void onClick(View v){
if(v.getId() == R.id.setting_layout_fname){
final EditText edittext = new EditText(getApplicationContext());
alertdialog.setTitle("First Name");
alertdialog.setMessage("Enter your firstname: ");
alertdialog.setView(edittext);
alertdialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
final String fname = edittext.getText().toString();
Toast.makeText(setting.this, fname, Toast.LENGTH_SHORT).show();
setting.this.input_fname.setText( fname, TextView.BufferType.EDITABLE);
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alertdialog.create().show();
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/activity_setting_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:divider="?android:dividerHorizontal"
android:showDividers="middle"
android:padding="10dp">
<LinearLayout
android:id="@+id/setting_layout_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
android:clickable="true">
<TextView
android:layout_height="wrap_content"
android:layout_weight="25"
android:clickable="false"
android:text="Firstname"
android:textStyle="bold"
android:layout_width="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="@+id/setting_textLayout_fname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="75"
android:clickable="false">
<android.support.design.widget.TextInputEditText
android:id="@+id/setting_edittext_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
答案 1 :(得分:0)