0

我有以下代码来设置EditText标题栏(主流程)的值,但它失败并且没有分配任何值。

你能告诉我怎么setText做吗?

以下是我的代码(我的主活动)

@SuppressWarnings("deprecation")
public class MainActivity extends Activity
{
    private AsyncTask <Void , Void, Void> mRegisterTask ;
    private String userid;
    private String orderid;
    ...


    View inflatedView = getLayoutInflater().inflate(R.layout.activity_main, null);
    final EditText s = (EditText) inflatedView.findViewById(R.id.textView2);
    userid = "99999"; 
    s.setText(userid);

活动主

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:background="@drawable/bg_with_header"
android:padding="0dp"
tools:context=".MainActivity" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="0dp"
    android:padding="0dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="0dp"
        android:orientation="vertical"
        android:padding="0dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="82dp"
            android:layout_margin="0dp"
            android:layout_marginRight="50dp"
            android:orientation="horizontal"
            android:padding="0dp" >

            <ImageView
                android:id="@+id/logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="0dp"
                android:src="@drawable/header_tv1231231231231bnvlogo" />

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="82dp"
                android:layout_gravity="center"
                android:layout_toLeftOf="@+id/headerLogin"                    
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="300dp"
                    android:layout_height="86dp"
                    android:layout_marginBottom="0dp"
                    android:layout_marginLeft="0dp"
                    android:layout_marginRight="0dp"
                    android:showDividers="none"
                    android:tabStripEnabled="false" />
            </HorizontalScrollView>

            <RelativeLayout
                android:id="@+id/headerLogin"
                android:layout_width="wrap_content"
                android:layout_height="82dp"
                android:orientation="horizontal" >

                <EditText
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/txtbg"
                    android:ems="10"
                    android:enabled="false"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:padding=" 5dp"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#BDBDBD" />

                <EditText
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/textView1"
                    android:background="@drawable/txtbg"
                    android:ems="10"
                    android:enabled="false"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:padding="5dp"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#BDBDBD" />

                <TextView
                    android:id="@+id/textVieasas2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textVieasas"
                    android:layout_alignBottom="@+id/textVieasas"
                    android:layout_toRightOf="@+id/textView2"
                    android:text="Last Login Date"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#FFFFFF" />

                <TextView
                    android:id="@+id/textVieasas"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/textView2"
                    android:layout_alignLeft="@+id/textView2"
                    android:text="Sales Code"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#FFFFFF" />
            </RelativeLayout>
        </LinearLayout>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="0dp"
            android:padding="0dp" />
    </LinearLayout>
</TabHost>

4

2 回答 2

1

这样做

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
                EditText s = (EditText) findViewById(R.id.textView2);
                userid = "99999"; 
                s.setText(userid);

                // or

               View inflatedView = getLayoutInflater().inflate(R.layout.activity_main, null);
               final EditText s = (EditText) inflatedView.findViewById(R.id.textView2);
               userid = "99999"; 
               s.setText(userid);
               setContentView(inflatedView);

    }
于 2013-09-02T06:50:44.910 回答
1

您也可以简单地使用它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText s = (EditText) findViewById(R.id.textView2);
    userid = "99999"; 
    s.setText(userid);

}
于 2013-09-02T06:57:33.703 回答