如何解决从另一个布局访问 TextView
我想显示一个对话框(其中包含另一个资源文件的外观)但根据 JSON 文件更改(TextView 的)名称,但我无权访问 java 的其他资源文件布局从第一个活动
userName 在文本视图的 2eme 布局中定义,但我没有任何访问权限
package com.example.randomchatonline;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class page1 extends AppCompatActivity {
String userNameReceive = null;
String userNameRandom = null;
Button contactBtn,searchBtn;
String x1 = null,x2 = null;
private String URL_DATA="http://192.168.0.104/PHP/(Model)searchRandom.inc.php/?userName=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
Bundle userNameSend = this.getIntent().getExtras();
if(userNameSend != null){
userNameReceive = userNameSend.getString("userNameSend"); }
((myUser) this.getApplication()).setNameOfUser(userNameReceive);
contactBtn = (Button) findViewById(R.id.contactBtn);
searchBtn = (Button) findViewById(R.id.searchBtn);
URL_DATA=URL_DATA + userNameReceive;
contactBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userNameSend = userNameReceive;
Intent intent = new Intent(page1.this,contactList.class);
startActivity(intent);
}
});
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyCustomAlertDialog();
}
});
}
public void MyCustomAlertDialog(){
final Dialog MyDialog = new Dialog(page1.this);
MyDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
MyDialog.setContentView(R.layout.customdialog);
StringRequest stringRequest = new StringRequest(Request.Method.GET,URL_DATA,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//System.out.println(lastMsgId);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("res");
String userNameToFind;
String gender;
//for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(0);
userNameToFind = o.getString("userNameToFind");
gender = o.getString("gender");
userName = userNameToFind; //userName is defined in a othel xml file
//} //endForLoop
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Button chat = (Button)MyDialog.findViewById(R.id.chat);
Button next = (Button)MyDialog.findViewById(R.id.next);
chat.setEnabled(true);
next.setEnabled(true);
chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"go to chat",Toast.LENGTH_LONG).show();
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialog.cancel();
MyCustomAlertDialog();
}
});
MyDialog.show();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。