如何从Android中动态创建的编辑文字获得价值? [英] How to get value from dynamically created edit text in Android?

查看:294
本文介绍了如何从Android中动态创建的编辑文字获得价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Android中动态创建的EditText获取价值?



这是我的动态视图(XML文件)

 < LinearLayout 
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =horizo​​ntal>

< EditText
android:id =@ + id / et_waypoint
android:layout_width =match_parent
android:layout_height =wrap_content
android:layout_weight =1
android:background =@ drawable / background
android:hint =Way Points
android:padding =10dp
android:textSize =16sp/>

< Button
android:id =@ + id / btn_waypoint
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =查找/>
< / LinearLayout>

我正在使用 LayoutInflater ,因为我想完美的设计布局,这不是我通过 setLayoutParams 完成的,所以这是我使用 LayoutInflater 的原因。 >

这里是我的代码:

  LayoutInflater l = getLayoutInflater(); 
final LinearLayout mainActivity =(LinearLayout)findViewById(R.id.dynamic);
final View view = l.inflate(R.layout.dynamic_layout_waypoints,null);
buttonWayPoints =(Button)view.findViewById(R.id.btn_waypoint);
editTextWayPoints =(EditText)view.findViewById(R.id.et_waypoint);
mainActivity.addView(editTextWayPoints);

我是Android的新手。大多数人建议此解决方案,但对我来说无效,问题是当我实现此代码时,我的 ADD新的编辑文本按钮不回复我。



这是我试图获取插入的值,但是返回上一次创建的编辑文本的值: p>

  public Cursor getPerson(String Query){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query,null);
return c;
}

sb是 StringBuilder sb; ,w是 String w;

  String quaryWayPoints =select * from route_table其中trip_id =+ t; 
Cursor s = myDb.getPerson(quaryWayPoints);
int count3 = s.getCount(); (int j = 0; j< count3; j ++)
{
s.moveToNext();
w = s.getString(s.getColumnIndex(waypoints));
// se.append(w +。+00);
}
trip_name.setText(n);
trip_date.setText(d);
sb.append(w);
}
trip_route.setText(sb.toString());


解决方案

虽然我没有弄清楚你的问题是的,我仍然会尽力帮助。
这个代码是什么,点击顶部的按钮,一个新的布局(一个你想要的)将在运行时添加到活动屏幕,即一个带有按钮的edittext,当你点击该按钮时,将显示具有相应编辑文本值的吐司。因此解决了获得动态添加edittext的价值的问题



MainActivity

  public class MainActivity extends AppCompatActivity {
Button generateET;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay =(LinearLayout)findViewById(R.id.dynamic);
generateET =(Button)findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add,null);
按钮buttonWayPoints =(Button)viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints =(EditText)viewToAdd.findViewById (R.id.et_waypoint);

buttonWayPoints.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if (editTextWayPoints.getText()。toString()!= null){
Toast.makeText(MainActivity.this,editTextWayPoints.getText()。toString(),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this ,找不到文本,Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);

}
});

}
}

activity_main.xml

 <?xml version =1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:app =http://schemas.android.com/apk/res -auto
xmlns:tools =http://schemas.android.com/tools
android:layout_width =match_parent
android:layout_height =match_parent
工具:上下文= com.techmahindra.stackques.MainActivity >

< Button
android:id =@ + id / generateBtn
android:layout_width =match_parent
android:layout_height =wrap_content
android:layout_alignParentTop =true
android:text =add editText To layout/>

< ScrollView
android:layout_below =@ + id / generateBtn

android:layout_width =match_parent
android:layout_height = wrap_content
android:fillViewport =true>

< LinearLayout
android:id =@ + id / dynamic
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =vertical
>< / LinearLayout>
< / ScrollView>

< / RelativeLayout>

to_add.xml (将在运行时膨胀的布局) p>

 < LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:app =http://schemas.android.com/apk/res-auto
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation = 水平 >

< EditText
android:id =@ + id / et_waypoint
android:layout_width =match_parent
android:layout_height =wrap_content
android:layout_weight =1

android:hint =Way Points
android:padding =10dp
android:textSize =16sp/>

< Button
android:id =@ + id / btn_waypoint
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =查找/>
< / LinearLayout>


How to get value from dynamically created EditText in Android?

This is my dynamic view (XML file)

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_waypoint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/background"
            android:hint="Way Points"
            android:padding="10dp"
            android:textSize="16sp" />

        <Button
            android:id="@+id/btn_waypoint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Find" />
    </LinearLayout> 

And I am using LayoutInflater because I want perfect Designed layout which is not accomplish by me via setLayoutParams so this is the reason I am using LayoutInflater.

And here is my code:

  LayoutInflater l = getLayoutInflater();
                    final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
                    final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
                    buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
                    editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
 mainActivity.addView(editTextWayPoints);

I am new to Android. Most of people suggest this solution but it's not worked for me, the issue is when I implement this code my ADD NEW EDIT TEXT BUTTON not respond me.

This is how I am trying to get inserted value but it returns value of only last created edit text:

public Cursor getPerson(String Query) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(Query, null);
        return c;
    }

sb is StringBuilder sb;, w is String w;

String quaryWayPoints = "select * from route_table where trip_id = " + t;
            Cursor s = myDb.getPerson(quaryWayPoints);
            int count3 = s.getCount();
            for (int j = 0; j < count3; j++) {
                s.moveToNext();
                w = s.getString(s.getColumnIndex("waypoints"));
                // se.append(w + "." + "00");
            }
            trip_name.setText(n);
            trip_date.setText(d);
            sb.append(w);
        }
        trip_route.setText(sb.toString());

解决方案

Although, I didn't get what exactly your question is, still I'll try to help. What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext

MainActivity

public class MainActivity extends AppCompatActivity {
    Button generateET;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
        generateET = (Button) findViewById(R.id.generateBtn);
        generateET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater l = getLayoutInflater();
                final View viewToAdd = l.inflate(R.layout.to_add, null);
                Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
                final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);

                buttonWayPoints.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (editTextWayPoints.getText().toString() != null) {
                            Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                myLinearLay.addView(viewToAdd);

            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.techmahindra.stackques.MainActivity">

    <Button
        android:id="@+id/generateBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="add editText To layout" />

    <ScrollView
        android:layout_below="@+id/generateBtn"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/dynamic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
          ></LinearLayout>
    </ScrollView>

</RelativeLayout>

to_add.xml(layout which will be inflated at runtime)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/et_waypoint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:hint="Way Points"
        android:padding="10dp"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn_waypoint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Find" />
</LinearLayout>

这篇关于如何从Android中动态创建的编辑文字获得价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆