简体   繁体   English

setText()不起作用

[英]setText() is not working

I have a one EditText in my xml.It throws no exception but the text "kailash" I had set in EditText is not displayed. 我的xml中有一个EditText抛出没有异常,但是我没有显示我在EditText设置的文本"kailash" It shows me empty EditText . 它显示我空的EditText In debugging I show that compiler executing the tx1.setText("kailash"); 在调试中我表明编译器正在执行tx1.setText("kailash"); But dont know this doesnt work.I have a simple MainActivity in that there is a search button and onclick of search button I am trying to display a dialog .Please help me. 但不知道这不起作用。我有一个简单的MainActivity ,因为有一个搜索按钮和onclick search按钮我试图显示一个对话框。请帮助我。 This is my code:-- 这是我的代码: -

Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.search_dialog);
RelativeLayout layout = (RelativeLayout) MainActivity.this.getLayoutInflater()
                                      .inflate(R.layout.search_dialog, null);
EditText txt1 = (EditText) layout.findViewById(R.id.txt1);
txt1.setText("kailash");
dialog.show();

this is my xml code: 这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"  >

 <EditText
        android:id="@+id/txt1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/editbox_background"
    android:textSize="16dp" />
</RelativeLayout>
final EditText txt1 = (EditText) findViewById(R.id.txt1);
txt1.setText("kailash");

call findViewById on your Activity Class, not a RelativeLayout you just inflate (for no reason IMHO) 在你的活动类上调用findViewById ,而不是你刚刚膨胀的RelativeLayout(无论如何恕我直言)

you can't change final - it's a constant. 你无法改变final - 这是一个常数。 Define it without the final modifier, set your text, then define other instance as final 在没有最终修饰符的情况下定义它,设置文本,然后将其他实例定义为final

Back to the original post. 回到原帖。

Kailash, it looks like the code first sets the dialog's content view from the XML layout: dialog.setContentView(R.layout.search_dialog); Kailash,看起来代码首先从XML布局设置对话框的内容视图:dialog.setContentView(R.layout.search_dialog);

... and then creates ANOTHER instance of the same resource by inflating it into another object, layout. ...然后通过将其膨胀到另一个对象布局来创建相同资源的另一个实例。

Then you modify the text view in the latter, but Android shows the former view. 然后修改后者中的文本视图,但Android显示前一个视图。

How about this? 这个怎么样?

EditText txt1 = (EditText) dialog .findViewById(R.id.txt1); EditText txt1 =(EditText) 对话框 .findViewById(R.id.txt1);

Well, though successful, this line swap looks like magic. 好吧,虽然成功,但这次换线看起来很神奇。 May be just an implementation specific coincidence - regression prone. 可能只是一个特定的实现巧合 - 易于回归。 It may depend on the inflater internals, where the layout objects are cached, etc. You still inflate into an object instance potentially different from the content view. 它可能取决于inflater内部,布局对象被缓存等等。您仍然会膨胀到可能与内容视图不同的对象实例。 If it happens to be the same in the current implementation, is there any guarantee it will always be the same? 如果它在当前实现中恰好相同,是否有任何保证它始终是相同的?

OTOH, dialog .findViewById seems to be more robust: you just get a reference to a component in the dialog - exactly like you would do with an Activity. OTOH, dialog .findViewById似乎更健壮:你只需要在对话框中获得对组件的引用 - 就像你对Activity一样。

Am I missing anything? 我错过了什么吗?

set text like this 像这样设置文本

View vv;        
    LayoutInflater  inflater  =  (LayoutInflater) YourClassName.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
    vv = inflater.inflate(R.layout.rowrelative, null);      
    EditText  edt =(EditText) vv.findViewById(R.id.editText1);
    edt.setText("Ankit");       
    this.addContentView(vv, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

Here you need to change Your Class name and Rowxml File Name 在这里,您需要更改您的类名称和Rowxml文件名

actually the solution is very simple i was putting this code : 实际上解决方案很简单,我把这个代码:

dialog.setContentView(R.layout.search_dialog);
RelativeLayout layout = (RelativeLayout) MainActivity.this.getLayoutInflater()
                                  .inflate(R.layout.search_dialog, null);

instead of this code: 而不是这段代码:

 RelativeLayout layout = (RelativeLayout) MainActivity.this.getLayoutInflater()
                                  .inflate(R.layout.search_dialog, null);
 dialog.setContentView(R.layout.search_dialog);

as activity has another layout in its setcontentView() method so it does not recognize the another layout so u should first load the layout by inflater and put it in dialog's setContentView method to make it work. 因为activity在其setcontentView()方法中有另一个布局,所以它不能识别另一个布局,因此你应首先通过inflater加载布局并将其放在对话框的setContentView方法中以使其工作。 hope it helps somebody . 希望它能帮到某个人。 and thanks for ur support. 并感谢你的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM