我在将文本设置为 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>
这是处理我的警报对话框的代码。
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 的编辑文本中。