如何在android中设置动态字符串作为用户选择?

时间:2015-02-11 04:37:59

标签: android

我是Android的初学者。我正在尝试创建一个应用程序,当用户点击按钮他应该去另一个活动时,让我们说如果用户点击按钮1他看到信息-A的新活动,如果他按下按钮2他看到同一活动中的信息-B他看到了信息A.

我该如何设置呢。

3 个答案:

答案 0 :(得分:3)

如果我正确理解你的问题,这是一个解决方案。

您可以使用Intent的putExtras方法将键/值对中的数据传递给下一个活动。

将以下内容添加到活动1布局:

<Button 
    android:id="@+id/btnA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button A" 
    android:onClick="goToActivityFromButtonA" />

 <Button 
    android:id="@+id/btnB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/btnA"
    android:text="Button B" 
    android:onClick="goToActivityFromButtonB" />

将以下方法添加到活动1:

public void goToActivityFromButtonA(View view) {
    Intent intent = new Intent(this, ActivityTwo.class);
    intent.putExtra("buttonData", "You clicked button A");
    startActivity(intent);
}

public void goToActivityFromButtonB(View view) {
    Intent intent = new Intent(this, ActivityTwo.class);
    intent.putExtra("buttonData", "You clicked button B");
    startActivity(intent);
}

将以下内容添加到活动2布局:

 <TextView
    android:id="@+id/txtText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

将以下内容添加到活动2的onCreate方法中:

TextView txtText = (TextView)findViewById(R.id.txtText);
txtText.setText(getIntent().getExtras().getString("buttonData"));

答案 1 :(得分:0)

答案 2 :(得分:0)

    you have first create ui for first activity like this

    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".FirstActivity" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="77dp"
            android:text="Button 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="110dp"
            android:layout_marginLeft="38dp"
            android:layout_toRightOf="@+id/button1"
            android:text="Button 2" />

    </RelativeLayout>



    ui of second activity like this 




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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="second actvity" />

    </LinearLayout>



activity first code where i set index with intent for both button click(you also put string double ling and message with intent)


package com.example.teststart;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        b1=(Button)findViewById(R.id.button1);
        b2=(Button)findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id=v.getId();
        switch(id){
        case R.id.button1:
            Intent i1=new Intent(FirstActivity.this,secondactivity.class);
            i1.putExtra("one", 1);
            startActivity(i1);
        break;
        case R.id.button2:
            Intent i2=new Intent(FirstActivity.this,secondactivity.class);
            i2.putExtra("one", 2);
            startActivity(i2);
            break;
        }
    }


}




and second activity i get that integer value to identify which button is clicked and fire operation on that condition


package com.example.teststart;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class secondactivity extends Activity{
    TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactvity);
    tv=(TextView)findViewById(R.id.textView1);
    Bundle extras = getIntent().getExtras(); 
     int index = extras.getInt("one");
     if(index==1){
         tv.setText("nformation-A "+index);
     }else if(index==2){
         tv.setText("nformation-B "+index);
     }

}
}