【问题标题】:Access string_array from activity and send selected item to another activity in android从活动访问 string_array 并将所选项目发送到 android 中的另一个活动
【发布时间】:2020-02-19 05:14:06
【问题描述】:

朋友们,今天我的问题是关于 string_array。在我的活动中,我创建了两个按钮。现在,在 (strings.xml) 上,我创建了包含两个项目的字符串数组名称,并且我插入了一些文本,如下所示。在每个按钮上单击 i想单独访问每个项目。例如,在按钮 1 上单击显示第一个项目,在按钮 2 上单击给我第二个项目。您能否给我按钮单击的代码,因为我对如何访问项目一无所知。我创建了一个 textview 活动,每次点击都显示我的项目。到目前为止,我的代码运行良好,但我需要帮助我添加必须添加的额外代码。请帮助我。

// Button activity page
android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <Button
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="50dp"
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:text="@string/OKILA"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="160dp"
        android:layout_marginRight="50dp"
        android:text="1"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>
//strings.xml
    <string-array name="chapters">
        <item>this is tes1</item>
        <item>this is test2</item>
    </string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class Lipok extends AppCompatActivity implements View.OnClickListener {
    Toolbar mActionBarToolbar;
    Button btnOne;
    Button btnTwo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipok);
        mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
        setSupportActionBar(mActionBarToolbar);
        getSupportActionBar().setTitle("CHAPTERS");
    String[] chapters=getResources().getStringArray(R.array.chapters);

        btnOne = findViewById(R.id.btn1);
        btnTwo = findViewById(R.id.button1);        

        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String text="";
        switch(v.getId()){
            case R.id.btn1 : {
                text = getResources().getStringArray(R.array.chapters)[0];
                break;
            }
            case R.id.button1 : {
                text = getResources().getStringArray(R.array.chapters)[1];
                break;
            }
        }


        Intent intent = new Intent(Lipok.this,lipokchapters.class);
        intent.putExtra("DATA", text);
        startActivity(intent);
    }
}

//textview activity page
package com.Aolai.temeshilai;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class lipokchapters extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipokchapters);
        textView=findViewById(R.id.textv);
        String text= getIntent().getStringExtra("Data");
        textView.setText(text);

    }
}

【问题讨论】:

  • 那么你要显示的文本视图在不同的活动中?
  • 是差异活动。我在下面尝试了您的代码,但这里有问题“Text.setText(text);” setText 无法解析方法。
  • 其他活动呢?
  • 我已经更新了,请检查。
  • 错误是什么?

标签: android arrays button


【解决方案1】:

试试这个:

xml文件

android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <Button
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="50dp"
        android:id="@+id/OKILA"
        android:layout_width="wrap_content"
        android:text="@string/OKILA"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="160dp"
        android:layout_marginRight="50dp"
        android:text="1"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

字符串.xml

    <string-array name="chapters">
        <item>this is tes1</item>
        <item>this is test2</item>
    </string-array>

主类

public class Lipok extends AppCompatActivity implements View.OnClickListener {
    Toolbar mActionBarToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipok);
        mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
        setSupportActionBar(mActionBarToolbar);
        getSupportActionBar().setTitle("CHAPTERS");

    }

    @Override
    public void onClick(View v) {
         String text="";
         switch(v.getId()){
              case R.id.OKILA : {
                   text = getResources().getStringArray(R.array.testArray)[0];
                   break;
              }
              case R.id.button1 : {
                   text = getResources().getStringArray(R.array.testArray)[1];
                   break;
              }
         }
         Toast.makeText(this, text, Toast.LENGTH_LONG).show();

             Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
             intent.putExtra("DATA", text);
             startActivity(intent);

         // Or else you can do whatever you want to do here with that text
    }
}

然后在你的其他活动中

String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);

【讨论】:

  • 我的 textview 在另一个活动中,所以我也需要链接到该活动
  • 你不能像那样链接它,你必须有意识地将数据发送到那个屏幕,然后在那里设置文本@newbie77
  • 很抱歉打扰您,但我如何有意将数据发送到该屏幕。因为您上面的代码不起作用。我想我也必须在我的其他 textview 活动上编写一些代码
  • "your_textview_name.setText(text)" 当我将它替换为我的 textview 的名称时,它不接受。也许因为它在另一个活动中。我没有在我的 textview 活动上写任何代码。跨度>
  • 要将数据发送到其他屏幕,请点击此链接:stackoverflow.com/questions/2091465/…
【解决方案2】:

这是Android的基础知识,所以在回答之前我建议你去 在发布到 Stackoberflow 之前通过基本教程。

现在,

您可以通过以下方式访问您的 string_array 中的项目。

String[] chapters= getResources().getStringArray(R.array.chapters);

现在,这里有一些伪代码可能会对你有所帮助。

btn1.setOnClickListener(

your_text_view.setText(chapters[0])

)
//Here 0 means your first item, likewise you can access items by their index in 
 array

【讨论】:

    猜你喜欢
    • 2012-08-04
    • 2018-03-22
    • 1970-01-01
    • 2013-07-03
    • 2016-01-31
    • 2012-04-15
    • 1970-01-01
    • 2014-09-09
    • 2014-09-06
    相关资源
    最近更新 更多