0

我在将文本设置为 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 的编辑文本中。

4

2 回答 2

0

嗯……我想通了。当我创建与旧元素略有相同的新 xml 类时,它可以完美地与 editText 或 textView 一起使用。所以我认为我犯了一些错误并忽略了它。所以我想我应该回答我自己的问题。

于 2018-02-01T07:59:26.287 回答
0

代码有2个edittext,第一个是alertdialog中以编程方式创建的editText,第二个是在xml文件中创建的editText。

我想要做的就是 setText 到 xml 中的 editText,其值来自 alertdialog 中的 editText。我可以从 alertdialog 中的 edittext 获取值,但无法在 xml 文件中将 setText 设置为 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>
于 2018-02-01T03:58:28.877 回答