在同一个Activity中传递字符串

时间:2013-03-28 10:10:34

标签: android string

如何在相同的活动中传递字符串?我有很多按钮所以我在open()???中使用了getstring。所有在同一个Activity ..在这个活动中我已经使用了来自另一个活动的意图传递字符串。

View view1 = findViewById(R.id.v1);
view1.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {

            Bundle i = new Bundle();
            i.putString("key1", title1); // Key1
            i.putString("key2", link1); // Key2
            open();
        }
    });

View viewnowvideo= findViewById(R.id.tvnowvideo);
viewnowvideo.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
            Bundle i = new Bundle();
            i.putString("key1", title2); // Key1
            i.putString("key2", link2);  // Key2
            open();
             }
    });
}

private void open() {
Bundle i = new Bundle();
String titleok = i.getString("key1"); //// Key1

final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
    dialog.setTitle(titleok);
    dialog.setContentView(R.layout.popup);
    Button button = (Button) dialog.findViewById(R.id.open2);
    button.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
        Bundle i = new Bundle();
        String linkok = i.getString("key2"); //// Key2
        Uri location = Uri.parse(linkok);
        Intent intent = new Intent(Intent.ACTION_VIEW, location);
        intent.setPackage("com.dfgfdg.dfgdfg");
        startActivity(intent);

        }
    });
  }

3 个答案:

答案 0 :(得分:3)

只需在课程级别声明此行String titleok = i.getString("key1"); //// Key1,您就不需要再次将其传递给相同的活动&试。

这基本上称为变量范围。如果您在类级别声明任何变量,则可以使用所有方法&内容很容易。

示例,

public class myClass
{
    private String hello="I am available to whole class";

    private void test()
    {
         System.out.println ( hello );
    }

    private class myInnerclass
    {
         private void innnerMethod()
         {
             System.out.println ( hello );  // direct accessible from outer class
         }
    }
}

答案 1 :(得分:1)

只需在Bundle方法中添加open()参数:

private void open(Bundle args) {
   String titleOk = args.getString("key1");
   ...
}

答案 2 :(得分:0)

我认为在open方法中你可以传递bundle并将该bundle分配给你的open方法包。希望这可以帮到你

  class MainActivity extends Activity{
   Bundle bundle;
   public void onCreate(Bundle savedInstanceState) {
  bundle=new Bundle();   
  View view1 = findViewById(R.id.v1);
  view1.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {


        bundle.putString("key1", title1); // Key1
        bundle.putString("key2", link1); // Key2
        open();
    }
   });

 View viewnowvideo= findViewById(R.id.tvnowvideo);
  viewnowvideo.setOnClickListener(new View.OnClickListener() {
   @Override
 public void onClick(View view) {

        bundle.putString("key1", title2); // Key1
        bundle.putString("key2", link2);  // Key2
        open();
         }
});
}

 private void open() {

String titleok = bundle.getString("key1"); //// Key1

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(titleok);
dialog.setContentView(R.layout.popup);
Button button = (Button) dialog.findViewById(R.id.open2);
button.setOnClickListener(new OnClickListener() {
@Override
    public void onClick(View v) {

    String linkok = bundle.getString("key2"); //// Key2
    Uri location = Uri.parse(linkok);
    Intent intent = new Intent(Intent.ACTION_VIEW, location);
    intent.setPackage("com.dfgfdg.dfgdfg");
    startActivity(intent);

    }
});

}